Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to find every occurrence of a substring by a function?

Reply
Thread Tools

How to find every occurrence of a substring by a function?

 
 
Peng Yu
Guest
Posts: n/a
 
      01-25-2010
Example 3b on the following page use a loop to find all substring.

http://perlmeme.org/howtos/perlfunc/index_function.html

I'm wondering if there is a function in perl library that can give me
back all the matches, just like findall in python (see section 3.3 in
the following page).

http://www.amk.ca/python/howto/regex/
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      01-25-2010
Peng Yu wrote:
> Example 3b on the following page use a loop to find all substring.
>
> http://perlmeme.org/howtos/perlfunc/index_function.html
>
> I'm wondering if there is a function in perl library that can give me
> back all the matches, just like findall in python (see section 3.3 in
> the following page).
>
> http://www.amk.ca/python/howto/regex/


Sure:

my @findall = $string =~ /pattern/g;




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-25-2010
Peng Yu <(E-Mail Removed)> wrote:
>Example 3b on the following page use a loop to find all substring.
>
>http://perlmeme.org/howtos/perlfunc/index_function.html
>
>I'm wondering if there is a function in perl library that can give me
>back all the matches,


Maybe I misunderstand what you are looking for but it appears to me that
the g-modifier in list context does exactly what you want. From 'perldoc
perlretut':

In list context, "//g" returns a list of matched groupings, or
if there are no groupings, a list of matches to the whole
regexp.

jue
 
Reply With Quote
 
Peng Yu
Guest
Posts: n/a
 
      01-25-2010
On Jan 24, 8:49*pm, "John W. Krahn" <(E-Mail Removed)> wrote:
> Peng Yu wrote:
> > Example 3b on the following page use a loop to find all substring.

>
> >http://perlmeme.org/howtos/perlfunc/index_function.html

>
> > I'm wondering if there is a function in perl library that can give me
> > back all the matches, just like findall in python (see section 3.3 in
> > the following page).

>
> >http://www.amk.ca/python/howto/regex/

>
> Sure:
>
> my @findall = $string =~ /pattern/g;


I think that I didn't make it clear. I want the location of the match.

For example, for the following variables, I want get an array that has
the number 0 and 7. You command gives an array of two same strings
'abc' and 'abc'.

$string="abcdefgabc";
$substring="abc";


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-25-2010
Peng Yu wrote:
> On Jan 24, 8:49 pm, "John W. Krahn" <(E-Mail Removed)> wrote:
>> Peng Yu wrote:
>>> Example 3b on the following page use a loop to find all substring.
>>> http://perlmeme.org/howtos/perlfunc/index_function.html
>>> I'm wondering if there is a function in perl library that can give me
>>> back all the matches, just like findall in python (see section 3.3 in
>>> the following page).
>>> http://www.amk.ca/python/howto/regex/

>> Sure:
>>
>> my @findall = $string =~ /pattern/g;

>
> I think that I didn't make it clear. I want the location of the match.


You said that you wanted something "just like findall" which according
to the page you referenced "Find all substrings where the RE matches,
and returns them as a list." which is exactly what the Perl code above does.


> For example, for the following variables, I want get an array that has
> the number 0 and 7. You command gives an array of two same strings
> 'abc' and 'abc'.
>
> $string="abcdefgabc";
> $substring="abc";


$ perl -le'
my $string = "abcdefgabc";
my $substring = "abc";
my @offsets;
{ use re "eval";
$string =~ /\Q$substring\E(??{ push @offsets, $-[ 0 ] })/g;
}
print "@offsets";
'
0 7




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-25-2010
Peng Yu <(E-Mail Removed)> wrote:
>On Jan 24, 8:49*pm, "John W. Krahn" <(E-Mail Removed)> wrote:
>> Peng Yu wrote:
>> > I'm wondering if there is a function in perl library that can give me
>> > back all the matches, just like findall in python (see section 3.3 in
>> > the following page).
>> >http://www.amk.ca/python/howto/regex/

>>
>> my @findall = $string =~ /pattern/g;

>
>I think that I didn't make it clear. I want the location of the match.


You wrote: "give me back all the matches". That is different from the
location of those matches.

And according to the documentation your Python findall() appears to
return the found matches, too, not their positions:

findall() Find all substrings where the RE matches, and
returns them as a list

jue
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Match every word except a substring T5in9tao Tsingtao Ruby 2 11-27-2007 05:19 PM
Find first occurrence of string and replace Registered User C Programming 6 10-18-2006 10:28 PM
Any tool in ASP to find out occurrence of a certain word or words Jack ASP General 8 09-19-2005 01:07 AM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM



Advertisments