我有一个像这样的字符串:
"text1 <text2> text3"
我只想抓取中的文字<>
。所以我需要得到text2
。我该怎么做?
您可以这样做:
String value = "text1 <text2> text3 <text4>";
Pattern pattern = Pattern.compile("<([^>]*)>");
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
输出:
text2
text4
回应更新:
假设您只知道要提取的一个值,那么Bohemian提出了一种更简单的方法,他提议继续进行以下操作:
String value = "text1 <text2> text3";
String target = value.replaceAll(".*<(.*)>.*", "$1");
System.out.println(target);
输出:
text2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句