java 正则表达式匹配全字 OR 运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11442179/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:09:00  来源:igfitidea点击:

Regular expression matching whole word OR operator

javaregex

提问by FirmView

I am trying to match full word from some lines, wanted to know how to use the OR in regex, If i use only one keyword, it works fine. Example,

我正在尝试匹配某些行中的完整单词,想知道如何在正则表达式中使用 OR,如果我只使用一个关键字,它工作正常。例子,

regex = ".*\b" + "KEYWORD1" + "\b.*";


String regex = ".*\b" + "KEYWORD1|KEYWORD2|KEYWORD3" + "\b.*";

    for (int i = start; i < end; i++) {           
        if (lines[i].matches(regex)) {
            System.out.println("Matches");
        }
    }

回答by Kendall Frey

You want:

你要:

String regex = ".*\b(KEYWORD1|KEYWORD2|KEYWORD3)\b.*";

Originally, your regex was being evaluated like this:

最初,您的正则表达式是这样评估的:

.*\bKEYWORD1
|
KEYWORD2
|
KEYWORD3\b.*

But you want:

但你想要:

.*\b
(
    KEYWORD1
    |
    KEYWORD2
    |
    KEYWORD3
)
\b.*

This cool toolcan help you analyse regexes and find bugs like this one.

这个很酷的工具可以帮助您分析正则表达式并找到像这样的错误。

回答by Andrew Clark

The pipe character |can be used as an OR operator, which is called alternation in regex.

管道符|可以用作 OR 运算符,在正则表达式中称为交替。

To get this to work properly in your example, you just need to create a group around the alternation to be sure that you are doing the OR only on the keywords you are interested in, for example:

要使其在您的示例中正常工作,您只需要围绕交替创建一个组,以确保您仅对您感兴趣的关键字执行 OR,例如:

String regex = ".*\b(KEYWORD1|KEYWORD2|KEYWORD3)\b.*";

What you currently have would mean .*\\bKEYWORD1 OR KEYWORD2 OR KEYWORD3\\b.*.

您目前拥有的将意味着什么.*\\bKEYWORD1 OR KEYWORD2 OR KEYWORD3\\b.*