您好,我在获取字符串的第三个元素 (F604080) 时遇到问题
<sourceDocumentId>AX02_APF604_F604080</sourceDocumentId>
我尝试过这个正则表达式和变体,但我可以设法得到 F604080。
(?<=\w+_)\w+(?=\<)
(?<=\w+_\w+_)\w+(?=\<)
...
任何帮助将不胜感激。 谢谢。
最佳答案
您不需要向后看或向前看,只需使用这个简单的正则表达式即可,
.*_(\w+)
和捕获组 1。
Java 代码,
public static void main(String[] args) {
String s = "<sourceDocumentId>AX02_APF604_F604080</sourceDocumentId>";
Pattern p = Pattern.compile(".*_(\\w+)");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1));
} else {
System.out.println("Didn't match");
}
}
按照您想要的方式打印此内容。
F604080
关于java - 正则表达式从字符串中获取第三个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53709689/