in reply to Regex to replace consecutive tokens

An alternative is the sledgehammer approach of retrying until there is nothing more:

1 while $s =~ s/,,/,0,/;

At least in this case, it's not hard to see that this approach will need at most two runs to replace all occurrences properly. Other, more complex regular expressions might need more thought, especially when the strings grow long. Rescanning a string might be more expensive than picking up a bit before where you last left off.

Replies are listed 'Best First'.
Re^2: Regex to replace consecutive tokens
by choroba (Cardinal) on Oct 18, 2013 at 12:47 UTC
    Adding /g might reduce the number of iterations.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Regex to replace consecutive tokens
by wjw (Priest) on Oct 18, 2013 at 14:44 UTC
    Sledgehammers are so cool....no muss, no fuss. Just pick it up, give it a swing, and watch your problem shatter... .

    ++sledgehammers

    ++ Corion


    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results.
      Sledgehammers are so cool.... give it a swing, and watch your problem shatter... .

      Just make sure that it hasn't got any Miley spittle on it.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.