in reply to Modifying the match position after each match

Can you simplify it to this?
use strict; use locale; my $string = "_'nada_komo_el_'sol_"; $string =~ s/(_)([^'])/$1'$2/g; print $string; __END__ _'nada_'komo_'el_'sol_

Update: or even:

$string =~ s/(_)[^']/$1'/g;

Replies are listed 'Best First'.
Re^2: Modifying the match position after each match
by ikegami (Patriarch) on Mar 23, 2010 at 21:44 UTC
    The solution you added in the update doesn't work.
    $string =~ s/(_)[^']/$1'/g;
    should be
    $string =~ s/(_)([^'])/$1'$2/g;
    That can also be written as
    $string =~ s/(_)(?=[^'])/$1'/g;
    and
    $string =~ s/_\K(?=[^'])/'/g; # 5.10+
Re^2: Modifying the match position after each match
by pat_mc (Pilgrim) on Mar 24, 2010 at 08:43 UTC
    No, sorry ... this won't work either. However, you could not know because my example indeed would permit the simplification you suggest. The regex cannot be simplified to (_)[^']) since a) single quotes can also occur inside a string and b) I only know that the end of the string has been reached when the next underscore is reached. Another input string might be my $string = "_na'da_komo_el_so'l_"