简体   繁体   中英

How do I stop this from being greedy

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?

Replace (.*) with (.*?) to make it non-greedy.

Here's a good explanation: Greedy and Non-Greedy Matches

这样做。

^/workorder/([^/]*)/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM