Perl regular expression questions?

gaiacarra

New member
1. How do I get the number of times a pattern was matched in the text?

2. How do I retrieve the text of the n'th match? IE, if I'm searching for four letter words, and I want to get the second, fourth, and tenth strings that were matched by my pattern, how would I do that?
 
1. Google will provide the answer
2. There's no easy way. You have to write a loop and keep your own counter.

my $i = 0;
while ($string =~ m/pattern/g)
{
if (++$i == 2)
{
# this is the second match
}
# etc.
}
 
1. Google will provide the answer
2. There's no easy way. You have to write a loop and keep your own counter.

my $i = 0;
while ($string =~ m/pattern/g)
{
if (++$i == 2)
{
# this is the second match
}
# etc.
}
 
1. Google will provide the answer
2. There's no easy way. You have to write a loop and keep your own counter.

my $i = 0;
while ($string =~ m/pattern/g)
{
if (++$i == 2)
{
# this is the second match
}
# etc.
}
 
Back
Top