将字符串与 PCRE 中的多个模式进行匹配
我们如何将字符串与 PCRE 中的多个模式进行匹配? 有时我需要匹配这个字符串
subj1 = "9112345678 put car details of the car";
patt1 = "(\\d+) ([a-z]+) ([a-z]+) (.+)";
,主题可以像这样
subj2 = "9112345678 put car";
匹配模式,
patt2 = "(\\d+) ([a-z]+) ([a-z]+)";
因为主题是动态的并且不知道先验想要“或”这两个模式并且想要将主题与复合模式进行匹配。 像这样的东西 子匹配(patt1 或 patt2) 我们可以在 PCRE 中做到这一点吗?
how do we match a string against multiple patters in PCRE ? I need to match this string
subj1 = "9112345678 put car details of the car";
patt1 = "(\\d+) ([a-z]+) ([a-z]+) (.+)";
some times the subject can be like this
subj2 = "9112345678 put car";
which is matching pattern
patt2 = "(\\d+) ([a-z]+) ([a-z]+)";
since the subject is dynamic and not known a prior would like to "or" these 2 patterns and want to match the subject against a composite pattern.
some thing like
sub matching (patt1 or patt2)
can we do this in PCRE ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将最后一个块设置为可选:
如果您还需要允许“9112345678 put”,则添加更多可选组:
如果您想维护对捕获组的良好顺序引用并且如果您的 PCRE 引擎确实是 PC,那么您可以使用聚类组代替一些捕获组:
感谢 Kobi 好心建议这个变体。在此版本中,匹配:
将产生:
"9112345678"
in$1
"put"
in$2
“car”
in$3
“details of the car”
in$4
这可能比解释所有内容更容易处理额外的嵌套。
You just need to make the last chunk optional:
And if you also need to allow "9112345678 put" then add more optional groups:
If you want to maintain nice sequential references to your capture groups and if your PCRE engine really is PC, then you can use clustering groups in place of some of the capturing groups:
Thanks go to Kobi for kindly suggesting this variant. With this version, matching against:
will yield:
"9112345678"
in$1
"put"
in$2
"car"
in$3
"details of the car"
in$4
And that's probably easier to deal with than accounting for all the extra nesting.
您可以使用或运算符
|
。类似于([az]+)|([1-9]+)
。You can use the or operator
|
. Something like([a-z]+)|([1-9]+)
.