[英]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.