Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is there a command or package to search for all instances of asubstring in a string?

Reply
Thread Tools

Is there a command or package to search for all instances of asubstring in a string?

 
 
Peng Yu
Guest
Posts: n/a
 
      11-30-2008
Hi,

Can somebody let me know if there is a command or function to find all
the occurance of a substring in a string? I can write a program to do
this. I just want to know what is the best way to do this.

Thanks,
Peng
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-30-2008
Peng Yu wrote:
>
> Can somebody let me know if there is a command or function to find all
> the occurance of a substring in a string? I can write a program to do
> this. I just want to know what is the best way to do this.


perldoc -f m
perldoc -f index
perldoc -f rindex


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      11-30-2008
Peng Yu <(E-Mail Removed)> wrote:

> Can somebody let me know if there is a command or function to find all
> the occurance of a substring in a string?



What does "find" mean when you say it?

If string="aaa" and substring="aa", should it "find" the substring 1 or 2 times?


> I just want to know what is the best way to do this.



That depends entirely on what "this" is...

If "this" is:

find all of the starting indexes of a substring in a larger string

then to find non-overlapping substrings:

push @indexes, pos($string) - length $substr, while $string =~ /$substr/g;

or to find overlapping substrings:

for (my $pos = -1; {
$pos = index $string, $substr, $pos+1;
last if $pos == -1;
push @indexes, $pos;
}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-30-2008
Peng Yu <(E-Mail Removed)> wrote:
>Can somebody let me know if there is a command or function to find all
>the occurance of a substring in a string? I can write a program to do
>this. I just want to know what is the best way to do this.


perldoc -f index

and keep looking ( aka looping)

jue
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      11-30-2008
[A complimentary Cc of this posting was sent to
Tad J McClellan
<(E-Mail Removed)>], who wrote in article <(E-Mail Removed)>:
> find all of the starting indexes of a substring in a larger string
>
> then to find non-overlapping substrings:
>
> push @indexes, pos($string) - length $substr, while $string =~ /$substr/g;


One should keep in mind that overlapping substrings can be done in the
same way:

push @indexes, pos($string) - length $substr, while $string =~ /(?=$substr)/g;

# As above, $substr should not contain metachars; otherwise \Q\E may
be needed...

Hope this helps,
Ilya
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
Is there any command line editing package? haomiao Ruby 2 07-25-2007 12:39 AM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM



Advertisments