Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Conditional regex

by Anonymous Monk
on Feb 17, 2013 at 18:35 UTC ( #1019185=note: print w/ replies, xml ) Need Help??


in reply to Conditional regex

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

Employ Basic debugging checklist or

add use re 'debug'; to see what the regex engine is doing, and why your match fails

then adjust your pos invocation

Also, your code wont run as is -- come on :) use feature or whatever

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

It does , you just can't have space in condition, it has to be (?(?{, otherwise you couldn't distinguish between some constructs


Comment on Re: Conditional regex
Download Code
Re^2: Conditional regex
by Anonymous Monk on Feb 17, 2013 at 18:49 UTC

    perlre says

    (?(condition)yes-pattern|no-pattern)

    (?(condition)yes-pattern)

    Conditional expression. Matches yes-pattern if condition yields a true value, matches no-pattern otherwise. A missing pattern always matches.

Re^2: Conditional regex
by AnomalousMonk (Abbot) on Feb 17, 2013 at 19:30 UTC
    It does , you just can't have space ... otherwise you couldn't distinguish between some constructs

    Further to Anonymonk's point: This is common. Eliminating the extraneous space around  ? in the example below allows this pedestrian regex to compile and work as expected.

    >perl -wMstrict -le "print 'match' if 'xxx' =~ m{ ( ? : x ) }xms; " Sequence (? ...) not recognized in regex...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2015-07-04 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls