获取匹配组时出现“IllegalStateException:找不到匹配项”

时间:2015-07-17 08:29:16

标签: java regex

我使用以下代码从txt中提取街道地址,州,城市和邮政编码。

public boolean parseGoogleAddress(Iterator<WebElement> iter)
{
    //String streetAddressRegex = "(?<=at\\s)\\d{3,5}\\s\\S*\\s\\w*.*?(?=,)";
    String stateRegex = "(?<=\\s)[A-Z]{2}(?=\\s)";
    String cityRegex = "\"(?<=,)\\s.{2,25}(?=,)\"";
    String zipRegex = "\"(?<=\\s)\\d\\d\\d\\d\\d\\]]\\]\"";

    String streetAddress;
    String state;
    String city;
    String zip;

    Pattern streetAddressPattern = Pattern.compile("(?<=at\\s)\\d{3,5}\\s\\S*\\s\\w*.*?(?=,)");
    Pattern statePattern = Pattern.compile(stateRegex);
    Pattern cityPattern = Pattern.compile(cityRegex);
    Pattern zipPattern = Pattern.compile(zipRegex);



    while(iter.hasNext()) {
        WebElement webelementAddressList = iter.next();

        String singleAddress = webelementAddressList.getText();

        System.out.println("Value of singleAddress: " + singleAddress);

        Matcher streetAddressMatch = streetAddressPattern.matcher(singleAddress);
        Matcher stateMatch = statePattern.matcher(singleAddress);
        Matcher cityMatch = cityPattern.matcher(singleAddress);
        Matcher zipMatch = zipPattern.matcher(singleAddress);

        streetAddress = streetAddressMatch.group();
        System.out.println(streetAddress);


        System.out.println("streetAddressMatch: " + streetAddressMatch.group(0) + " stateMatch: " + stateMatch.group() + "cityMatch: " + cityMatch.group() + "zipMatch: " + zipMatch.group());
        break;
        }

    return true;

}

"itr"只包含一个类似于下面提到的地址列表。

我尝试将变量更改为streetAddressRegex的普通正则表达式(正如您在代码中看到的那样),但我仍然没有得到结果。

每次我运行它都会得到这样的结果:

Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)
at java.util.regex.Matcher.group(Unknown Source)
at ParseAddress.parseGoogleAddress(ParseAddress.java:45)
at ScrapeAddress.GoogleScrapeAddress(ScrapeAddress.java:73)
at Main.main(Main.java:46)

正则表达式模式已经过测试,可以自行运行。

传递给查找正则表达式模式的文本/地址的示例是:

    Find Salvation Army Thrift Store at 269 Washington St, West Warwick, RI 02893-5910. Call them at (401) 828-6301

我将.group(0)更改为.group()但仍然无效。

我是否错误地编译了模式?请指教。

由于

1 个答案:

答案 0 :(得分:3)

问题在于这一行:

@{
    Dictionary<int, List<int>> dictDestinations = (Dictionary<int, List<int>>)ViewData["AssociatedDestinations"];
}

您之前未致电streetAddress = streetAddressMatch.group(); group()致电streetAddress.find()