Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Why does this simple grouping regex not match?

by december (Pilgrim)
on Feb 28, 2011 at 13:30 UTC ( #890557=perlquestion: print w/replies, xml ) Need Help??

december has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

A quick question:

Why doesn't the first regex match while the second does?

$ perl -e '$_ = "(b)."; /\((?=a|b|c)\)/ && do { print "match!\n"; };' $ perl -e '$_ = "(b)."; /\((?=a|b|c)/ && do { print "match!\n"; };' match!

It's probably very simple and it has something to do with the use of grouping brackets instead of regular capturing brackets, but I don't seem to get why one works and the other doesn't it's Monday and so far my brain has refused to accept that fact.

Thanks for any pointers!

EDIT: It's a typo. I overlooked the fact I typed an equal sign instead of a colon, accidently making it a look-ahead regex. Monday just isn't my day. Sorry folks!

Replies are listed 'Best First'.
Re: Why does this simple grouping regex not match?
by moritz (Cardinal) on Feb 28, 2011 at 13:34 UTC
    (?= ...) is not a "simple grouping", but a look-ahead (ie it looks for the presence of the regex within the group, but doesn't consume any characters).

    Use (?: ... ) for just grouping.

    (Update: fixed markup)

      I'm an idiot. It's a typo, I meant to use a colon in my code. I just kept on copying the same typo.

      Sorry folks!

        a healthy colon is vital to good health and vitality
Re: Why does this simple grouping regex not match?
by fisher (Priest) on Feb 28, 2011 at 13:38 UTC
    I'm not sure how it iterprets exactly, but I think you need actually
    perl -e '$_ = "(b)."; /\([abc]\)/ && do { print "match!\n"; };'
    perl -e '$_ = "(b)."; /\((a|b|c)\)/ && do { print "match!\n"; };'
Re: Why does this simple grouping regex not match?
by Anonymous Monk on Feb 28, 2011 at 13:58 UTC
    use re 'debug';
    perl -Mre=debug -e '$_ = q!(b).!; /\((?=a|b|c)\)/ && do { print qq{mat +ch! $&\n}; };' perl -Mre=debug -e '$_ = q!(b).!; /\((?=a|b|c)/ && do { print qq{match +! $&\n}; };'

      Interesting... didn't know about that. Noted for future use. Thanks!

Re: Why does this simple grouping regex not match?
by umasuresh (Hermit) on Feb 28, 2011 at 13:35 UTC
    Is this what you are looking for?
    $ perl -e '$_ = "(b)."; /(\(?=a|b|c\))/ && do { print "match!\n"; };' match!
    The outer parenthesis captures the match and the inner should be escaped!
    Never mind, didn't pay attention to the atomic grouping!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://890557]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-01-18 20:42 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (54 votes). Check out past polls.