in reply to Re^3: Modifying the match position after each match
in thread Modifying the match position after each match

I think (mostly because I've only been following this thread from the sidelines) that this won't "work" if there is no underscore at the end of the string:

#! /usr/bin/perl -w use strict; use locale; for my $s ( @{[ "_'nada_komo_el_'sol_", "_na'da_komo_el_so'l_", "_na'da_komo_el_sol" ]} ) { print $s; $s =~ s/(_)([^'][^_]+?)/$1'$2/g; print " : $s\n"; } __END__ > perl -w tmp.pl _'nada_komo_el_'sol_ : _'nada_'komo_'el_'sol_ _na'da_komo_el_so'l_ : _'na'da_'komo_'el_'so'l_ _na'da_komo_el_sol : _'na'da_'komo_'el_'sol

My solution would be to use a zero-width lookahead to ensure but not match the following underscore:

$s =~ s/(_)([^_']+?)(?=_)/$1'$2/g;

... but again, as I haven't been closely following, I'm not sure where my approach will fail the specifications.

Replies are listed 'Best First'.
Re^5: Modifying the match position after each match
by BrowserUk (Patriarch) on Mar 24, 2010 at 13:29 UTC

    You could be right, it isn't entirely clear.

    I based my interpretation of the spec on the post above where he says:

    b) I only know that the end of the string has been reached when the next underscore is reached.

    And the fact that in all his examples, the strings do end with underscore.


    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.