|Pathologically Eclectic Rubbish Lister|
Re: How to make only two of three substitutions?by nothingmuch (Priest)
|on Oct 15, 2002 at 21:44 UTC||Need Help??|
Something which I believe may be of some interest to you is the \G zero width assertion.
The above code first finds a slash, and saves it's position in the lvalue acccessible by pos($_). Because the m//g is not performed in list context, the slashes are not all pushed to a list and returned. Scalar/void context m//g lets you step through the string, in an iterational fashion.
Next, the substitution is performed so that the match has to succeed \G. \G is the place where the last /g left off. /g modification on s/// substitutes everything regardless of context. The first slash found, and it's position noted, we proceed to find a minimal anything, followed by a slash. Once we find that, we replace it with the minimal anything, and remember the position. We then search onward for more slashes, until no match can be made.
This specific example is a bit flawed, because it invloves a lot of copying, in theory, and it is also not the best choice for your specific example, but I just thought it may be useful to know.
perlre contains more info.
zz zZ Z Z #!perl