Just another Perl shrine | |
PerlMonks |
Progressive matching w/substitutionsby argv (Pilgrim) |
on Aug 09, 2008 at 03:35 UTC ( [id://703225]=perlquestion: print w/replies, xml ) | Need Help?? |
argv has asked for the wisdom of the Perl Monks concerning the following question:
The ORA Programming Perl book discusses progressive matching, but only uses m// matching, leaving the question of whether s/// works, too. One would assume so, but if you use the sample code and modify it to use s/// substitutions, none of the examples work anymore.
I also tried using combinations of uses that start with m// in the "while" loop, but using s/// inside the block to modify the string, and then reset the regex position using pos, but again, nothing works. here's sample code:
The desired result should look like this:
Instead, it produces "abc def(3)def(2)def(1)abc abc abc" and the reason is obvious: the regex always starts back at 0, and it does this no matter what I do. (The above sample code is but one of a variety of attempts.) In addition to someone solving the specific problem with the code, I'd like a more specific explanation for what the perl rules are concerning progressive matching and the use of substitutions. Is it different than m//? Is it undefined? Is there a specific method documented that I'm not seeing? And why doesn't the above pos call set the position accordingly? UPDATE: Incidentally, I know I can get what I want using this code: while ($string =~ s/(abc) ([^d])/$1 def($x) $2/i) ... The point is to understand the proper way of doing progressive matching with substitutions... my so-called 'workaround' won't work in the actual program I'm using, as I can't be sure what the next letter(pattern) would be after the 'abc' match.
Dan Heller http://www.danheller.com
Back to
Seekers of Perl Wisdom
|
|