in reply to Regexp and substitution question
As other people have noted, a subsequent successful match resets the captured values. However, this doesn't have to create a problem; you can use the list behavior of captures in a regex to save them for reuse.
#!/usr/bin/perl -w use strict; my $line="One two three four five"; my @chunks; if(@chunks = $line =~ m/(one)\s(two)\s(three)\s(four)\s(five)/i){ $chunks[0] =~ s/One/ONE/; # Change the first chunk $chunks[1] =~ s/two/tWo/; # Change the second chunk # ... } print "@chunks\n";
Output:
ONE tWo three four five
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regexp and substitution question
by pemungkah (Priest) on Aug 29, 2010 at 00:27 UTC |