Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: A regex that only matches at offset that are multiples of a given N?

by 7stud (Deacon)
on Feb 16, 2013 at 05:41 UTC ( #1019012=note: print w/ replies, xml ) Need Help??


in reply to A regex that only matches at offset that are multiples of a given N?

Help!

If I use the construct (?(condition)yes-pattern) in a regex, and the condition is (?{1}), i.e. always true, the output is as expected:

my $str = 'bxAybz'; while ( $str =~ /(?(?{1})(b[xyz]))/g ) { say 'yes'; say $1; } --output:-- yes bx yes bz

But when I use the condition (?{pos() % 2 == 0}), I expect the same output, but I don't get it:

my $str = 'bxAybz'; while ( $str =~ /(?(?{pos() % 2 == 0})(b[xyz]))/g ) { say 'yes'; say $1; } --output:-- yes bx yes Use of uninitialized value $1 in say at 2.pl line 9. yes bz

Three matches?

Also, I notice the x modifier doesn't work with a conditional pattern:

my $str = 'bxAybz'; while ( $str =~ / (? (?{1}) (b[xyz]) ) /gx ) { say 'yes'; say $1; } --output:-- Sequence (? ...) not recognized in regex; marked by <-- HERE in m/ (? <-- HERE (?{1}) (b[xyz]) ) / at 2.pl line 12.


Comment on Re: A regex that only matches at offset that are multiples of a given N?
Select or Download Code
Re^2: A regex that only matches at offset that are multiples of a given N?
by BrowserUk (Pope) on Feb 16, 2013 at 07:21 UTC

    First, I'm not the right person to be asking this question of. Whatever regex expertise I once had is well out of date. There is a whole bunch of stuff I've never done anyting with. However, this is my interpretation of the decidedly unclear documentation:

    Three matches?

    The (what appears to be called) zero-length switch assertion, appears to succeed, whenever the condition part succeeds; regardless of whether the yes pattern (or no pattern, if present) succeed.

    So with 6 characters in your string, and a condition that restricts matching to every second character, the overall match succeeds 3 times, even if the yes pattern only matches at 2 of those positions. Hence your output.

    Also, I notice the x modifier doesn't work with a conditional pattern:

    It appears that you are breaking up an indivisible token with the whitespace you've used. This works:

    C:\test>perl -M5.010 -w my $str = 'bxAybz'; while ( $str =~ / (?(?{1}) (b[xyz]) ) /gx ) { say 'yes'; say $1; } ^Z yes bx yes bz

    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.
Re^2: A regex that only matches at offset that are multiples of a given N?
by tobyink (Abbot) on Feb 16, 2013 at 07:57 UTC

    /(?(?{pos() % 2 == 0})(b[xyz]))/g

    I think you want to explicitly back-track in the no condition...

    /(?(?{pos() % 2 == 0})(b[xyz])(*FAIL))/g
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-12-21 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (107 votes), past polls