Reputation: 2933
I have the following regex:
#^/workorder/(.*)/#
Provided the following URI's:
/workorder/archive/2/
The $matches variable from preg_match() is returning archive/2/
Why doesn't the last /
in the regex stop the (.*)
from being greedy? What do I add to make the regex stop when the /
delimiter is found?
Upvotes: 2
Views: 144
Reputation: 14089
To make it not greedy (lazy) use
^/workorder/(.*?)/
But this will use backtracking which is not good for performance. Use
^/workorder/([^/]*)/
To have your cake and eat it fast
Upvotes: 2
Reputation: 16462
Replace (.*)
with (.*?)
to make it non-greedy.
Here's a good explanation: Greedy and Non-Greedy Matches
Upvotes: 3