我的代码包含以下参数:
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
for url2
m.find()== true
,方法返回224
答案 0 :(得分:0)
你的正则表达式也正常。
示例代码:
String url = "http://biz-test.waze.com/providers/224/billing_details";
Pattern p = Pattern.compile("(?<=providers/)\\d+");
Matcher m = p.matcher(url);
if(m.find()){
System.out.println(m.group()); //224
}
它在工作。 DEMO
将\d+
括在括号(\d+)
示例代码:
String url = "http://biz-test.waze.com/providers/224/billing_details";
Pattern p = Pattern.compile("(?<=providers/)(\\d+)");
Matcher m = p.matcher(url);
if(m.find()){
System.out.println(m.group(1)); // 224
}