0
我想匹配使用preg_match
具有以下的正則表達式多串的:正則表達式匹配了太多的字符串
#^/test/(.+?)(\/.+^/?)?$#
以下字符串的結果如下
匹配(應該匹配)
preg_match('#^/test/(.+?)(\/.+?)?$#', '/test/segment'); // true
preg_match('#^/test/(.+?)(\/.+?)?$#', '/test/segment/another-segment'); // true
不匹配(不應該匹配)
preg_match('#^/test/(.+?)(\/.+?)?$#', '/test'); // false
比賽(不應該匹配)
preg_match('#^/test/(.+?)(\/.+?)?$#', '/test/segment/another-segment/yet-another/segment'); // true
可有人告訴我如何使最後一個失敗?打破正則表達式,本質上它應該匹配一個字面值/test
,後面跟着一個必需的/something
和一個可選的/something
,但在下一個出現的/something
處停止。
希望是有道理的。
這點?第二個? – philipobenito
您可以替換它們,因爲兩者都不應該匹配斜線。 – dognose
這工作,謝謝,我會盡快接受時間限制... – philipobenito