简体   繁体   English

正则表达式解析街道名称

[英]Regex To Parse Street Names

I have the following code that I would like to use to capture street names from an address string. 我有以下代码,希望用于从地址字符串中捕获街道名称。

For an address of "77 ELM ST", the code below gives me "ELM" which is correct. 对于地址“ 77 ELM ST”,下面的代码给我“ ELM”,它是正确的。

For an address of "115 GEORGE APPLETON FOX ST" the code gives "APPLETON". 地址为“ 115 GEORGE APPLETON FOX ST”时,代码为“ APPLETON”。 The expected result should be " GEORGE APPLETON FOX" 预期结果应为“ GEORGE APPLETON FOX”

For example: 例如:

private static String ADDRESS_PATTERN = "^\\d*\\s*(\\w+\\s*)*ST$";

public String parseStreet(String address) {
    Pattern addressPattern = Pattern.compile(ADDRESS_PATTERN);
    Matcher matcher = addressPattern.matcher(address);
    boolean found = matcher.find();
    return found ? matcher.group(1).trim() : null;
}

What am I doing wrong? 我究竟做错了什么?

Put the capturing groups around the submatch you want to capture. 将捕获组放在要捕获的子匹配项周围。 For example: 例如:

"^\\d*\\s*((?:\\w+\\s*)*)ST$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM