Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想对带有可选和必需部分的 uri 字符串进行 preg_match ,但我无法弄清楚。
匹配示例:
/segment/(required)/segment(/optional)
我希望下面的两个字符串都与上面的匹配
/segment/required/segment/optional
和
/segment/reguired/segment
我知道政策是不为我写信,但我想不通,所以我想我至少会问。
问号使正则表达式中的前面的标记成为可选的。例如:colou?r同时匹配colour和color。
colou?r
colour
color
您可以通过使用圆括号将它们组合在一起并将问号放在右括号之后来使多个标记成为可选。例如:Nov(ember)?将匹配Nov和November。
Nov(ember)?
Nov
November
通过包含多个问号,您可以编写一个匹配许多备选方案的正则表达式。Feb(ruary)? 23(rd)?匹配February 23rd,和. February 23_Feb 23rdFeb 23
Feb(ruary)?
23(rd)?
February 23rd
February 23
Feb 23rd
Feb 23
来源:http ://www.regular-expressions.info/optional.html