【发布时间】:2014-08-29 09:40:08
【问题描述】:
我正在尝试编写一个匹配字符串后跟可选模式的正则表达式。
这是我要匹配match/string的三个输入字符串的示例
/any/path/match/string
/any/path/match/string/pattern
/any/path/match/string/pattern/something/else
其中/any/path 和/pattern 是静态文本,match/string 可以是任何文本。
我试过了
print $1 if m,/any/path/(.+)(?=/pattern/)?,
但它匹配整个输入字符串,因为.+ 是贪婪的。
【问题讨论】:
-
(?=...)?这样的语法是无操作的。好吧...“后跟可选模式”没有多大意义。你想做什么?请举出具体例子。 -
基本上我正在尝试匹配“/any/path”和“/pattern”之间的文本,其中“/pattern”可能存在或不存在于输入字符串中。
-
问题在于,由于
match/string可能是“任何文本”,那么其中一个可能是/match/string/pattern/something/else。如果你正确定义了你想要做什么,那么代码会更加明显