Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Here is my problem ... I need to make substitutions on all occurrences of a pattern which occur between defined markers but nowhere else in the string, for example
my $text = 'An abitrary BEGIN_MARKER string which END_MARKER could contain BEGIN_MARKER just about END_MARKER anything' ;The markers always occur in pairs, there could be any number of pairs, and the markers themselves must be retained.
How can I make a greedy substitution between BEGIN_MARKER and END_MARKER, but nowhere else?
I guess it should be easy, but I have just spent 4 hours with Mastering Regular Expressions, Perl Cookbook and Programming Perl ... and I still can't figure it out.
The nearest I have got is
$text =~ s/(BEGIN_MARKER.*?)a(.*?END_MARKER)/$1 x $2/g;intended to substitute 'x' instead of 'i' (that is of course a simplification of what I am really trying to do).
It doesn't work -- it merely finds all marker pairs, but substitutes only the first occurence of 'i' between them.
I don't even know if I am close. Should I be trying to use a backreference? I'm new to all this.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Greedy Substitution Within a Defined Range
by BrowserUk (Patriarch) on Apr 21, 2004 at 13:19 UTC | |
by diotalevi (Canon) on Apr 21, 2004 at 13:35 UTC | |
by Anonymous Monk on Apr 21, 2004 at 19:17 UTC | |
|
Re: Greedy Substitution Within a Defined Range
by halley (Prior) on Apr 21, 2004 at 13:16 UTC | |
|
Re: Greedy Substitution Within a Defined Range
by borisz (Canon) on Apr 21, 2004 at 14:08 UTC | |
|
Re: Greedy Substitution Within a Defined Range
by matija (Priest) on Apr 21, 2004 at 13:25 UTC |