gpt4 book ai didi

java - "IllegalStateException: No match found"获取匹配组时

转载 作者:行者123 更新时间:2023-12-05 01:02:38 25 4
gpt4 key购买 nike

我正在使用以下代码从 txt 中提取街道地址、州、城市和 zip。

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() 但还是不行。

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

谢谢

最佳答案

问题是这一行:

streetAddress = streetAddressMatch.group();

您在调用 group() 之前没有调用 streetAddress.find()streetAddress.matches()

关于java - "IllegalStateException: No match found"获取匹配组时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com