Peng Yu wrote:
> On Jan 24, 8:49 pm, "John W. Krahn" <some...@example.com> 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