繁体   English   中英

如何使用Java中的正则表达式提取以下文本中的必需字符串?

[英]How to extract required string in below text using regular expressions in java?

我想在下面的文本的第一个和最后一个之间提取字符串。

方案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());
  }

暂无
暂无

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

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