[英]Java Pattern matching regex
I am doing a Pattern match the matcher.matches is coming as false, while the matcher.replaceAll actually finds the pattern and replaces it. 我正在做一个模式匹配matcher.matches是假的,而matcher.replaceAll实际上找到模式并替换它。 Also the matcher.group(1) is returning an exception.
matcher.group(1)也返回一个异常。
@Test
public void testname() throws Exception {
Pattern p = Pattern.compile("<DOCUMENT>(.*)</DOCUMENT>");
Matcher m = p.matcher("<RESPONSE><DOCUMENT>SDFS878SDF87DSF</DOCUMENT></RESPONSE>");
System.out.println("Why is this false=" + m.matches());
String s = m.replaceAll("HEY");
System.out.println("But replaceAll found it="+s);
}
I need the matcher.matches() to return true, and the matcher.group(1) to return "<DOCUMENT>SDFS878SDF87DSF</DOCUMENT>" 我需要matcher.matches()返回true,matcher.group(1)返回“<DOCUMENT> SDFS878SDF87DSF </ DOCUMENT>”
Thanks in advance for the help. 先谢谢您的帮助。
final Pattern pattern = Pattern.compile("<DOCUMENT>(.+?)</DOCUMENT>");
final Matcher matcher = pattern.matcher("<RESPONSE><DOCUMENT>SDFS878SDF87DSF</DOCUMENT></RESPONSE>");
if (matcher.find())
{
System.out.println(matcher.group(1));
// code to replace and inject new value between the <DOCUMENT> tags
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.