示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String data = "Shyam and you. You are 2.3 km away from home. Lakshmi and you. Ram and you. You are Mike. ";
Pattern pattern = Pattern.compile(
"\\s*((?:[^\\.]|(?:\\w+\\.)+\\w)*are.*?)(?:\\.\\s|\\.$)",
Pattern.DOTALL);
Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
输出:
You are 2.3 km away from home.
You are Mike.
我在执行上面的代码时得到了预期的输出。但问题是在使用更大的字符串测试相同的正则表达式时,它会显示溢出错误。我进行了大致相同的搜索,发现正则表达式中的 (A|B)* 之类的交替会导致问题。有没有办法解决这个问题?请帮忙。