Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Non-greedy regex behaves greedily

by thospel (Hermit)
on Nov 10, 2004 at 12:32 UTC ( #406663=note: print w/replies, xml ) Need Help??

in reply to Re^2: Non-greedy regex behaves greedily
in thread Non-greedy regex behaves greedily

Possibly, I don't know what your exact ideas are :-) At least .+? is not an optional match for one or more things.

But I've also noticed that a lot of people use non-greedy matching as if it's some sort of magical negative lookahead. It isn't. It will do whatever it takes to match, even if that means also eating one or more of the character(s) that directly follows the non-greedy match (and exiting the repeat at some later instance of that character(s) instead). If your regex has only one match of a non-fixed length you can usually get away with this, but especially if there are more than one them, the semantics can get unexpected.

If you don't want to match something, it's almost always better to just say that. So in your case that would be:

(also notice that greedy or non-greedy becomes irrelevant if you write it like this) It would of course still not work as expected (the + should be a * since matching 0 times should be allowed too), but at least the problem now will be not doing anything at all instead of changing too much. And it will keep working as expected even if you make your regex more complicated.

Replies are listed 'Best First'.
Re^4: Non-greedy regex behaves greedily
by fourmi (Scribe) on Nov 10, 2004 at 12:44 UTC
    ok, today i learned something interesting for a change!

    Thanks for the assistances!!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2020-10-29 22:51 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (274 votes). Check out past polls.