Keep It Simple, Stupid | |
PerlMonks |
Re: Replace part of a regex matchby monarch (Priest) |
on Dec 24, 2008 at 05:58 UTC ( [id://732419]=note: print w/replies, xml ) | Need Help?? |
I know that, in the past, I've just wanted to jam something into $1 - it seems a natural way of expressing what I want to do. Unfortunately Perl doesn't do it this way. So you've got to think of other means. And there are many approaches (see perlre).
One method might be to calculate where the match of your capture finishes, calculate where the capture started, and replace that portion of the string directly using substr. Example code:
The problem is that you have to use a look-ahead to prevent the pos function returning the end of the entire regexp match.. The other problem is that you have to use the /g (global) flag on your match to ensure the position is calculated. This code replaces the "277" with the word "moo". Try it!
In Section
Seekers of Perl Wisdom
|
|