|Syntactic Confectionery Delight|
Re: need a regular exdpression in perlby Loops (Curate)
|on Nov 20, 2014 at 08:24 UTC||Need Help??|
Your source string needs to be in single quotes, otherwise Perl will try to interpolate '@begin' and '@end' as arrays. You also don't want to put your replacement strings inside square brackets, that groups them as a single array reference inside of @replace, rather than as individual strings.
Anyway, this should get you started:
Usually regular expression substitutions are written as:s/<pattern>/<substitution text>/<modifiers>
But in my code above, the vertical bar (or pipe) symbol (|) is used instead. It's also split over 3 lines which is allowed when you use the "x" modifier.
The "e" modifier means that instead of substitution text, the given code will be executed for each @begin ... @end match. The code shown, removes the first element of the array and returns it for the current substitution. If the @array becomes empty, the literal value '[null]' will be used instead.
The "g" modifier causes the substitution to be done as many times as there are matches found in the string -- consuming another array element each time. Without it only the first substitution would be done.
And lastly the "s" modifier means that the period character in the pattern, will match newline characters since those appear in your example input string.