从其他字符串中提取某些字符串

时间:2018-04-18 05:23:38

标签: java regex string extract matcher

我自己创建了一个项目,我需要提取某个URL的字符串。 在我使用之前

https://www.amazon.de/gp/product/SOMETEXTHERE/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1

但是如果它在第6个 / 之后,我只能得到它。 我知道 matcher()会有一些东西,但我不能让它工作。

例如:

IReadOnlyCollection<IWebElement> elems = Driver.FindElements(By.XPath("//div[.='Test App']")).Where(e => e.Displayed).ToList();
elems.ElementAt(0).Click();

我想提取出SOMETEXTHERE。但是这个角色正在改变,所以我不能只说它总是一样的。 然后我需要SOMETEXT然后单独写一个字符串。

1 个答案:

答案 0 :(得分:2)

使用group(1)来检索product//ref之间的文字:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(".*product/(.*)/ref.*");
        Matcher matcher = pattern.matcher("https://www.amazon.de/gp/product/SOMETEXTHERE/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1");
        if (matcher.matches()) {
            System.out.println(matcher.group(1));
        }
    }
}