java为什么我的正则表达式find返回false?
我有以下参数的代码:
private int getIdFromUrlByRegex(String regex) {
String url = webDriver.getCurrentUrl();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
return m.find() ? Integer.valueOf(m.group()) : 0;
}
url1 = https://mysite/providers/224/billing_details
url2=https://myysite.com/providers/224
regex = "(?<=providers/)\d+"
对于url1,它评估:
m.find()==false
方法返回0而不是224
对于url2
m.find()==true
该方法返回224
# 1 楼答案
你的正则表达式也运行良好
示例代码:
它起作用了DEMO
通过将
\d+
括在括号内(\d+)
在组1处捕获它示例代码: