This bugged me for a while so I thought I'd mention the solution I found, as well as ask if there is a better way to do it:

Basically I wanted to do a rather complex substitution globally but /g did not work for me. The reason /g did not work is that I need part of the string that has already be used in order to do each match.

I guess a simple example will illustrate the problem better: in the string 'a1b1a2b1b2a3b1b2b3'db1b2 I want to move all the b\d+ following a\d+ to the end of the string and wrap them in (). In this case the simple s{(a\d+)(b\d+)(.*)}{$1$3($2)}g will not work: it will only move the first b, as the regexp engine has to go all the way to the end of the string to match the (.*) part.

There might be ways to do this in one substitution, and I'd like to hear about them, but in fact the real regexps are a bit more complex, so easy tricks are not likely to work, I have searched for them for a while ;--( The main problems are that the (a\d+) is of variable length, which prevents using a look behind (?<=a\d+), each (b\d+) has to be processed separately, and the final (.*) will go through the whole string anyway...

In any case here is the solution I found: use a while with an empty body: while( $string=~ s{(a\d+)(b\d+)(.*)}{$1$3($2)}){}. This works fine, is conceptually clean, but I don't like the empty {}, it looks just weird. Which makes me wonder if there is an other way to do this, one that would be blessed by better hackers than me?

Here is a toy script if you want to play with the problem:

#!/bin/perl -w use strict; while( my $string=<DATA>) { # this works while( $string=~ s{(a\d+)(b\d+)(.*)}{$1$3($2)}){} # this does not #$string=~ s{(?<=a\d+)(b\d+)(.*)}{$3($2)}g; print "string: $string\n"; } __DATA__ a1b1b2b3 a1b1a12b1b2a3b1b2b3db1b2

In reply to Real global substitution, what's the best way? by mirod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.