I need some Perl regular expression help. The following snippet of code:
use strict;
use warnings;
my $str = "In this example, A plus B equals C, D plus E plus F equals G and H plus I plus J plus K equals L";
my $word = "plus";
my @results = ();
1 while $str =~ s/(.{2}\b$word\b.{2})/push(@results,"$1\n")/e;
print @results;
Produces the following output:
A plus B D plus E 2 plus F H plus I 4 plus J 5 plus K
What I want to see is this, where a character already matched can appear in a new match in a different context:
A plus B D plus E E plus F H plus I I plus J J plus K
How do I change the regular expression to get this result? Thanks --- Dan
General advice: Don't use s/// when you want m//. Be specific in what you match.
The answer is pos:
#!/usr/bin/perl -l
use strict;
use warnings;
my $str = 'In this example, ' . 'A plus B equals C, ' .
'D plus E plus F equals G ' .
'and H plus I plus J plus K equals L';
my $word = "plus";
my @results;
while ( $str =~ /([A-Z] $word [A-Z])/g ) {
push @results, $1;
pos($str) -= 1;
}
print "'$_'" for @results;
Output:
C:\Temp> b 'A plus B' 'D plus E' 'E plus F' 'H plus I' 'I plus J' 'J plus K'
You can use a m//g instead of s/// and assign to the pos function to rewind the match location before the second term:
use strict;
use warnings;
my $str = 'In this example, A plus B equals C, D plus E plus F equals G and H plus I plus J plus K equals L';
my $word = 'plus';
my @results;
while ($str =~ /(.{2}\b$word\b(.{2}))/g) {
push @results, "$1\n";
pos $str -= length $2;
}
print @results;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With