in reply to regexp: non-capturing grouping in replacement possible?

You need look-around assertions, not non-capturing groups. See perlre, search for positive look-ahead assertion and zero-width positive look-behind assertion.

A substitution substitutions not only the captured part of the string, but the matched part of the string. Using zero-width assertion you can reduce the matched part of the string to the captured part.

  • Comment on Re: regexp: non-capturing grouping in replacement possible?

Replies are listed 'Best First'.
Re^2: regexp: non-capturing grouping in replacement possible?
by diweooy (Novice) on Nov 26, 2009 at 13:11 UTC
    I forgot to mention that I tried it but either I am doing something wrong or ...:
    $str =~ s/(?=hello kitty )(\d+)/sprintf "%d", $1/e;
    does not do anything. it is probably me but I don't see it.
      ?= is look ahead. try ?<= (look behind)
        That's right. The regex engine walks the string from left to right, so looking left of a pattern is a look-behind, looking right of a pattern is a look-ahead.
        thanks :). working great. $str =~ s/^(?<=hello kitty )(\d\d*)(?=x\d+.*)/sprintf "%d", $1/e;;