如何在“感谢”和“真诚”之间获得刺痛文本
String emailtext= "Dear (RECIPIENT_PREFIX) (RECIPIENT_FIRST_NAME) (RECIPIENT_LAST_NAME),
Thank you for being a part.....................more text ......
Thank you for your time. Filling out these sections with ...more text.... for which you are chosen.
Sincerely,
xyz
adresss"
我试过了:
String pattern = "(Thank)((.|\n)*)(?=Sincerely,)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(emailtext);
if (m.find( )) {
System.out.println("Found value:" + m.group(0));
System.out.println("Found value:" + m.group(1));
System.out.println("Found value:" + m.group(2));
} else {
System.out.println("NO MATCH");
}
错误显示:
java.lang.StackOverflowError的 at java.util.regex.Pattern $ CharProperty.match(Pattern.java:3692) 在java.util.regex.Pattern $ Branch.match(Pattern.java:4502) at java.util.regex.Pattern $ GroupHead.match(Pattern.java:4556) 在java.util.regex.Pattern $ Loop.match(Pattern.java:4683) 在java.util.regex.Pattern $ GroupTail.match(Pattern.java:4615) at java.util.regex.Pattern $ BranchConn.match(Pattern.java:4466)at java.util.regex.Pattern $ CharProperty.match(Pattern.java:3694) 在java.util.regex.Pattern $ Branch.match(Pattern.java:4502) at java.util.regex.Pattern $ GroupHead.match(Pattern.java:4556) 在java.util.regex.Pattern $ Loop.match(Pattern.java:4683)
正确的ans应该显示:
"Thank you for being a part.....................more text ......
Thank you for your time. Filling out these sections with ...more text.... for which you are chosen."
答案 0 :(得分:1)
尝试一下:
String pattern = "(Thank)(.*)(?=Sincerely,)";
Pattern p = Pattern.compile(pattern,Pattern.DOTALL );
....
答案 1 :(得分:0)
那么你应该用
检查文本中是否存在这两个词CharSequence s1 = "Thank";
CharSequence s2 = "Sincerely";
If (emailtext.contains(s1) && emailtext.contains(s2))
因此它不能抛出那种异常,那么,你可以这样做:
String s = emailtext.substring(emailtext.lastIndexOf("Thank"),
emailtext.indexOf("Sincerely"));