在java中的两个字符串之间提取字符串 [英] Extract string between two strings in java
本文介绍了在java中的两个字符串之间提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在<%=和%>之间获取字符串,这是我的实现:
I try to get string between <%= and %>, here is my implementation:
String str = "ZZZZL <%= dsn %> AFFF <%= AFG %>";
Pattern pattern = Pattern.compile("<%=(.*?)%>");
String[] result = pattern.split(str);
System.out.println(Arrays.toString(result));
它返回
[ZZZZL , AFFF ]
但我的期望是:
[ dsn , AFG ]
我错在哪里以及如何纠正它?
Where am i wrong and how to correct it ?
推荐答案
你的模式很好。但是你不应该 split()
把它扔掉,你应该 find()
它。以下代码给出了您要查找的输出:
Your pattern is fine. But you shouldn't be split()
ting it away, you should find()
it. Following code gives the output you are looking for:
String str = "ZZZZL <%= dsn %> AFFF <%= AFG %>";
Pattern pattern = Pattern.compile("<%=(.*?)%>");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
这篇关于在java中的两个字符串之间提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文