【发布时间】:2019-06-26 11:57:12
【问题描述】:
我对正则表达式有点陌生,所以我有以下字符串:
[[ChromeDriver: chrome on LINUX (ff108507ea7a3598104c728cc453f299)] -> xpath: /html[1]/body[1]/div[3]/div[1]/header[1]/div[1]/div[1]/div[1]/div[1]/nav[1]/ul[1]/li[1]/a[1]] (class: sf-depth-1 menuparent ext sf-with-ul)
我想知道如何删除/html 之前的所有内容,所以我最终得到以下字符串:
/html[1]/body[1]/div[3]/div[1]/header[1]/div[1]/div[1]/div[1]/div[1]/nav[1] /ul[1]/li[1]/a[1]] (class: sf-depth-1 menuparent ext sf-with-ul)
我试过了,但没有成功:
Pattern pattern = Pattern.compile("/html.*");
Matcher matcher = pattern.matcher(absoluteXpath);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
在这里测试:
【问题讨论】:
-
为什么是
group(1)?模式中没有组。 -
根据你的链接看来你成功了。
-
如果你想使用正则表达式,你需要像这样在你的模式中创建一个组:Pattern.compile("(/html.*)")
标签: java regex string text-extraction