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

Re: RE greediness

by Anonymous Monk
on May 29, 2024 at 19:11 UTC ( [id://11159719]=note: print w/replies, xml ) Need Help??


in reply to RE greediness

The (.*?) in your second attempt is a non-greedy match, which means it will match the smallest possible substring that satisfies the pattern. In this case, it matches an empty string because the optional (?:b=(\w))? can also match the empty string. Use a positive lookahead assertion to ensure that the (.*?) matches up to the optional b=(\w) part, but doesn't include it:
/(a)(?:(.*?)(?:b=(\w)))?/

Replies are listed 'Best First'.
Re^2: RE greediness
by choroba (Cardinal) on May 29, 2024 at 19:25 UTC
    (?:...) is a non-capturing group, positive lookahead uses (?=...) which I can't find in the regex used.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        (?:...) is a non-capturing group, positive lookahead uses (?=...) which I can't find in the regex used.
      Sorry for the typo, meant to type this, but it's still wrong:
      /(a)(?=(.*?)(?:b=(\w)))?/
      This seems to work:
      /(a)(.*?)(?=b=(\w)|\z)/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-09-18 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (24 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.