Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Modifying the match position after each match

by pat_mc (Pilgrim)
on Mar 24, 2010 at 09:03 UTC ( #830487=note: print w/ replies, xml ) Need Help??


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

I believe that I need to include executable Perl code in the regex to reduce the match position variable after each matching string. Then ALL substrings of format _[^']+?_ should be matched:

#! /usr/bin/perl -w use strict; use locale; my $string1 = "_'nada_komo_el_'sol_"; my $string2 = "_na'da_komo_el_so'l_"; $string1 =~ s/(_)([^']+?)(_)(?{pos($string1) --})/$1'$2$3/go; $string2 =~ s/(_)([^']+?)(_)(?{pos($string2) --})/$1'$2$3/go; print "String 1: $string1 \n"; print "String 2: $string2 \n";

But somehow, this is not working for me and I get
--output:-- String 1: _'nada_'komo_el_'sol_ String 2: _na'da_'komo_el_so'l_
Any ideas as to why this is happening?

Your wisdom is much appreciated ...

Pat


Comment on Re^2: Modifying the match position after each match
Select or Download Code
Re^3: Modifying the match position after each match
by BrowserUk (Pope) on Mar 24, 2010 at 13:17 UTC

    You're working much to hard:

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

    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.

      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.

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://830487]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-12-27 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls