如何在Java中为单行文本匹配多个Regex模式? [英] How do you match multiple Regex patterns for a single line of text in Java?

查看:54
本文介绍了如何在Java中为单行文本匹配多个Regex模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有多个模式P1,P2,P3等.这些模式是不同的Regex模式,以匹配DATE的不同变体.

Lets say I have multiple patterns P1, P2, P3,, and so on. These patterns are different Regex patterns to match different variations of DATE.

如何在一段代码中最有效地将它们与相同的输入文本进行匹配.

How do I match these for the same input text most efficiently in a piece of code.

当然,我可以编写一个for()来逐个循环这些模式,但是有更好的方法吗?

Of course, I can write a for() to loop over these patterns one by one, but is there a better way to do this?

推荐答案

我认为您可以使用regex的 | 运算符并将不同的regex放在括号内,以将其视为一个完整的regex匹配.

I think you can use the | operator of the regex and put the different regexes in paranthesis to be considered one whole regex to be matched.

((P1)|(P2)|(P3)")

这篇关于如何在Java中为单行文本匹配多个Regex模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆