正则表达式可选组? [英] Regex Optional Groups?
本文介绍了正则表达式可选组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎对自己正在使用的preg_match正则表达式感到困惑,所以新鲜的眼睛和帮助将不胜感激.
I seem to have confused myself with a preg_match regex I'm doing, so fresh eyes and help would be appreciated.
我当前的正则表达式如下:
My current regex is as follows:
/<!--menu:start:\(([0-9])\,([0-9])\)-->(.*?)<!--menu:end-->/se
我想输入数字和冒号,例如:(1,4)是可选的,因此它将匹配:
I am looking to make the number input and colon e.g. :(1,4) optional, so it would match:
<!--menu:start--><!--menu:end-->
或
<!--menu:start:(0,3)--><!--menu:end-->
推荐答案
用不匹配的组括起来并将其设置为可选:(?:...)?
Enclose with a non matching group and set it to optional : (?:...)?
/<!--menu:start(?::\(([0-9])\,([0-9])\))?-->(.*?)<!--menu:end-->/se
这篇关于正则表达式可选组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文