如何在 Java 中为一行文本匹配多个 Regex 模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23536133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you match multiple Regex patterns for a single line of text in Java?
提问by London guy
Lets say I have multiple patterns P1, P2, P3,, and so on. These patterns are different Regex patterns to match different variations of DATE.
假设我有多个模式 P1、P2、P3 等等。这些模式是不同的 Regex 模式以匹配不同的 DATE 变体。
How do I match these for the same input text most efficiently in a piece of code.
如何在一段代码中最有效地将这些匹配到相同的输入文本。
Of course, I can write a for() to loop over these patterns one by one, but is there a better way to do this?
当然,我可以写一个 for() 来一个一个地循环这些模式,但是有没有更好的方法来做到这一点?
回答by anirudh
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)")
("(P1)|(P2)|(P3)")
回答by Mena
I would not use Pattern
s to match dates.
我不会使用Pattern
s 来匹配日期。
You have DateFormat
and SimpleDateFormat
classes to do so.
你有DateFormat
和SimpleDateFormat
类这样做。
This said, combining Patterns
can be done with the alternation(|
) operator on the String
representation of the Pattern
.
这就是说,Patterns
可以在 的表示上使用交替( |
) 运算符进行组合。String
Pattern
回答by Damian Leszczyński - Vash
You can use alternation (|)
operator to combine multiple patterns for your regexp.
您可以使用交替(|)
运算符为您的正则表达式组合多个模式。
But in case you have various input and you will have to convert them to instance of Date from a string. Then you must follow in a sequence and validate the input one by one. So single regexp may validate the input but it could not be used to any other logic.
但是,如果您有各种输入,并且必须将它们从字符串转换为 Date 的实例。然后,您必须按照顺序并一一验证输入。因此,单个正则表达式可以验证输入,但不能用于任何其他逻辑。
回答by fge
To complement the other answers...
为了补充其他答案......
You canwrite one big, hard-to-read pattern using the alternation operator:
您可以使用交替运算符编写一个大的、难以阅读的模式:
r1|r2|r3|...|rn
where r1
etc are themselves "fully-fleged" regexes.
其中r1
etc 本身就是“成熟的”正则表达式。
However you have to be careful about the orderof alternations: the first to match wins. That is, if the regex engine is not a POSIX regex engine but java.util.regex's engine isn't.
但是,您必须注意交替的顺序:第一个匹配的获胜。也就是说,如果 regex 引擎不是 POSIX regex 引擎但 java.util.regex 的引擎不是。
Therefore, with text catflap
, using regex:
因此,对于 text catflap
,使用正则表达式:
cat|catflap
Java will match cat
; a POSIX regex engine will match catflap
(the longest, leftmost match).
Java 将匹配cat
;POSIX 正则表达式引擎将匹配catflap
(最长的、最左边的匹配)。
Sticking with more individual, maintainable patterns is imho a better option.
坚持使用更多个性化、可维护的模式是更好的选择。