正则表达式在java中的两个字符串之间获取数据

时间:2017-12-20 07:31:54

标签: java regex

我正在尝试在两个字符串之间获取数据"帐户:"和"帐户"使用正则表达式从下面的字符串。

List<WebElement> rstar = dr.findElements(By.xpath("//*[@id='cm_cr-review_list']//div[@class='a-row']//a[@class='a-link-normal']//i"));
                String c;
                    for(WebElement erstar : rstar) {
                        c=erstar.getAttribute("innerText");

                        System.out.println(c);

我使用的正则表达式是:

    String str= "Order Confirmation Account: Sample Account ID: 1111"

我得到的实际输出是:

    Pattern pattern = Pattern.compile("Account:(.*)Account");
    Matcher matcher = pattern.matcher(str);
    System.out.println("Account name is:"+matcher.group(1));

预期输出为:

java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)
at WorkingProgram$1.run(WorkingProgram.java:102)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我的正则表达式出了什么问题?

1 个答案:

答案 0 :(得分:-1)

模式需要匹配整个String,所以

".*Account:(.*)Account.*"