将字符串与 PCRE 中的多个模式进行匹配

发布于 2024-11-09 06:39:37 字数 412 浏览 0 评论 0原文

我们如何将字符串与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-11-16 06:39:37

您只需将最后一个块设置为可选:

(\\d+) ([a-z]+) ([a-z]+)( (.+))?

如果您还需要允许“9112345678 put”,则添加更多可选组:

(\\d+) ([a-z]+)( ([a-z]+)( (.+))?)?

如果您想维护对捕获组的良好顺序引用并且如果您的 PCRE 引擎确实是 PC,那么您可以使用聚类组代替一些捕获组:

(\\d+) ([a-z]+)(?: ([a-z]+)(?: (.+))?)?

感谢 Kobi 好心建议这个变体。在此版本中,匹配:

9112345678 put car details of the car

将产生:

  • "9112345678" in $1
  • "put" in $2
  • “car” in $3
  • “details of the car” in $4

这可能比解释所有内容更容易处理额外的嵌套。

You just need to make the last chunk optional:

(\\d+) ([a-z]+) ([a-z]+)( (.+))?

And if you also need to allow "9112345678 put" then add more optional groups:

(\\d+) ([a-z]+)( ([a-z]+)( (.+))?)?

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:

(\\d+) ([a-z]+)(?: ([a-z]+)(?: (.+))?)?

Thanks go to Kobi for kindly suggesting this variant. With this version, matching against:

9112345678 put car details of the car

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.

三寸金莲 2024-11-16 06:39:37

您可以使用运算符|。类似于 ([az]+)|([1-9]+)

You can use the or operator |. Something like ([a-z]+)|([1-9]+) .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文