我正在执行此正则表达式代码,期望分组值为11,但是得到的是1.似乎该分组包含正确的正则表达式,用于在已知值之前获取一个或多个数字.我敢肯定这很简单,我似乎无法弄清楚.
String mydata = "P0Y0M0W0DT11H0M0S";
Pattern pattern = Pattern.compile("P.*(\\\\d+)H.*");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find()){
System.out.println(matcher.group(1));
}
解决方法:
尝试这个
public static void main(String a1[]) {
String mydata = "P0Y0M0W0DT11H0M0S";
Pattern pattern = Pattern.compile("P.*?(\\d+)H.*");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find()){
System.out.println(matcher.group(1));
}
}
输出量
11
标签:java,regex
来源: https://codeday.me/bug/20191027/1946356.html