我想提取下面文本中第一个和最后一个之间的字符串。
场景1:
"Select statement"+"\n"+
"first"+"\n"+
"I want to extract this" +"\n"+
"I want to extract this one too"+"\n"+
"last"+"\n"+
"end of the statement"+"\n"+
"first"+"\n"+
"I want to extract this" +"\n"+
"I want to extract this one too"+"\n"+
"last"+"\n"+
"end of the statement"
场景 2:
"Select statement"+"\n"+
"first"+"\n"+
"I want to extract this" +"\n"+
"last"+"\n"+
"end of the statement";
Java 中的正则表达式模式是什么来完成这项工作?模式应该是一种,但它应该在两种情况下都有效。感谢您的帮助。
最佳答案
您可以使用环视。
Matcher m = Pattern.compile("(?s)(?<=\\bfirst\\n).*?(?=\\n?\\blast\\b)").matcher(s);
if(m.find())
{
System.out.println(m.group());
}
关于java - 如何使用java中的正则表达式提取下面文本中所需的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32546405/