我有一个字符串<Thread> 'data we need' </Thread>
我想提取 'data we need'
.
我一直在使用这个,但没有任何结果:
Pattern patternThread = Pattern.compile("<Thread(.*?)/Thread>");
Matcher matcherThread = patternThread.matcher(a);
if (matcherThread.find()) {
System.out.println("Thread Oke");
System.out.println(matcherThread.group(1));
}
我知道问题是 "<" and the "/"
.
那么,有什么建议吗? 我已经尝试过“//”和“/” 难道是我的jdk太旧了? 7.2 我已经在oracle上搜索了解决方案,但仍然没有解决这个问题
最佳答案
您可以使用此正则表达式 <Thread>(.*?)</Thread>
:
Pattern patternThread = Pattern.compile("<Thread>(.*?)</Thread>");
Matcher matcherThread = patternThread.matcher("<Thread> 'data we need' </Thread>");
while (matcherThread.find()) {
System.out.println(matcherThread.group(1));
}
输出
'data we need'
- 您可以使用
while
如果您可以获得多个结果 - 您必须使用
matcherThread.group(1)
而不是matcherThread.group()
因为最后返回<Thread> 'data we need' </Thread>
关于java - 将正则表达式与 "<"等符号一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43604818/