Java 正则表达式匹配两个单词

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

Regex match two words

javaregex

提问by user3004412

I'm using Java regex and I need to match two words, but between these words can appear a space or another word, for e.g. "power energy", "power of energy", "power for energy". I have to obtain all variations of "power energy". Is it possible?

我正在使用 Java 正则表达式,我需要匹配两个词,但是在这些词之间可以出现一个空格或另一个词,例如“power energy”、“power of energy”、“power for energy”。我必须获得“动力能量”的所有变化。是否可以?

Further, I need to have something like "pow* ener*" to catch all the double words that start con pow and ener. For e.g "powers for energies".

此外,我需要像“pow* ener*”这样的东西来捕捉所有以 con pow 和 ener 开头的双字。例如“能量的力量”。

回答by Jorge Campos

Try this:

尝试这个:

/(?=power)(?=energy)/i

回答by megawac

Use a simple wild cardbetween the two words in the patterns:

在模式中的两个词之间使用简单的通配符

"power(.*?)energy"or just "power.*energy"

"power(.*?)energy"要不就 "power.*energy"

回答by Danny

The following

下列

Pattern pattern = Pattern.compile("(\bpow\w+)(.*?)(\bener\w+)");
Matcher matcher = pattern.matcher("Test powers test energies test.");
if(matcher.find()) {
    System.out.println(matcher.group());
}

Prints powers test energies. It should work with other variations that you posted. Obviously it will give you false positives if words start with pow, but that is what you seemed to want in the question.

打印powers test energies。它应该适用于您发布的其他变体。显然,如果单词以 开头,它会给你误报pow,但这正是你在问题中想要的。

回答by Casimir et Hippolyte

You can try this pattern:

你可以试试这个模式:

\bpowers?\s+(?:\w+\s+){0,2}energ(?:y|ies)\b

note: if you want you can allows more than two words between "power" and "energy" increasing (or decreasing) the quantifier value inside curly brackets.

注意:如果您愿意,您可以在“power”和“energy”之间允许两个以上的单词增加(或减少)大括号内的量词值。

\\bis a word boundary to avoid having other letters (or digit or underscore) after or before a word.

\\b是一个单词边界,以避免在单词之后或之前有其他字母(或数字或下划线)。

回答by Paolo Falabella

if you need to capture the words "power", "energy" and variations in groups, you can use:

如果您需要在组中捕获单词“power”、“energy”和变体,您可以使用:

(pow\S+\b).*(ener\S+\b)

In java:

在Java中:

Pattern pattern = Pattern.compile("(pow\S+\b).*(ener\S+\b)");
Matcher matcher = pattern.matcher("powers of energies");
if(matcher.find()) {
  int count = matcher.groupCount();
    for(int i=1;i<=count;i++){
      System.out.println(matcher.group(i));
    }

see http://ideone.com/eNY3br

http://ideone.com/eNY3br