Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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.

Replies are listed 'Best First'.
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?
[marto]: 4, if they're good they#ll make it to 5 :P
[1nickt]: Mpph didn;t have twins bu I remember thinking that the "terrible twos" was a misnomer as age 4 seemed to be much more difficult.
LanX 4 kids and wanting to upgrade to 5? wow! ! !
[1nickt]: My lads are 15 and 31 now and both drive me just as crazy as when little, just less frequently.
[LanX]: ;p
[marto]: LanX I'd rather commit sudoku :P
[marto]: trench humour folks, feeling rough.
[marto]: there is never a night when they sleep all the way through, but last night was something else

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2017-11-18 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?