简体   繁体   English

Java Regex,空字符串要匹配

[英]Java Regex,Empty String be matched

I wrote a regex in Java to match sentences containing some string like this: 我用Java写了一个正则表达式来匹配包含以下字符串的句子:

String regex = "((^|([.!?:] ))" + "[^!.?:]*?" + queryStr + ".*?" + "([!.?])|$)+?";

Then I use the regex to match my String, see below: 然后,我使用正则表达式来匹配我的String,如下所示:

Pattern pattern = Pattern.compile(regex);
String content = "Hello World!!!";
Matcher match = pattern.matcher(content);
int index = 0;
while(match.find(index))
{
   index = match.end() -1;
   System.out.println(match.group());
}

But the loop never ends, I suspect, because the regex matches empty string. 但是我怀疑循环永远不会结束,因为正则表达式匹配空字符串。 Apparently, my regex includes the String queryStr. 显然,我的正则表达式包含字符串queryStr。 So, I am confused with this. 所以,我对此感到困惑。 Can anyone help me to slove this? 谁能帮助我解决这个问题?

Your regex pattern looks like 您的正则表达式模式看起来像

((^|([.!?:] ))[^!.?:]*?Hello.*?([!.?])|$)+?

It contains 2 alternatives: 它包含2个替代方案:

  1. (^|([.!?:] ))[^!.?:]*?Hello.*?([!.?])
  2. $

So, the problem was that you were matching the end of string all the time in a loop. 因此,问题在于您一直在循​​环中匹配字符串的结尾。

Make this change: 进行以下更改:

String regex = "(^|[.!?:] )" + "[^!.?:]*?" + queryStr + ".*?" + "([!.?]+?|$)";

Now, it will look like 现在,它看起来像

(^|[.!?:] )[^!.?:]*?Hello.*?([!.?]+?|$)

And $ will be an alternative to [!.?]+? $将替代[!.?]+? only. 只要。

在此处输入图片说明

See demo on regex101.com . 请参阅regex101.com上的演示

Every term of your regex is optional. 正则表达式的每个术语都是可选的。

To prevent matching blank input, add this to the front of your regex: 为防止匹配空白输入,请将其添加到正则表达式的前面:

(?!$)

This is a look ahead that asserts the current position is not followed by end of input (ie "something" is following) 这是一个向前看的断言,断言当前位置后面没有输入结束(即紧跟着“某物”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM