正则表达式在java中提供额外的输出

时间:2014-02-21 09:22:14

标签: java regex regex-lookarounds

我的代码就像:

String try1 = " how abcd is a lake 3909 Witmer Road Niagara Falls NY 14305 and our adress is 120, 5th cross, 1st main, domlur, Bangalore 50071 nad 420, Fanboy Lane, NewYark, AS 12345";
String add1="( \\b+[0-9]{3,5}[, ]* (.*)[, ]* (.*)[, ]* [a-zA-Z]{2} [0-9]{5})";
Pattern p = Pattern.compile(add1);
Matcher m = p.matcher(try1);
if(m.find())
{ 
    System.out.println("Address ======> " + m.group());
}
else System.out.println("Address ======>Not found ");

我只想输出美国地址:

[(3909 Witmer Road Niagara Falls NY 14305) and (420, Fanboy Lane, NewYark, AS 12345)]

但输出如下:

(3909 Witmer Road Niagara Falls NY 14305 and our adress is 120, 5th cross, 1st main, domlur, Bangalore 50071 nad 420, Fanboy Lane, NewYark, AS 12345)

2 个答案:

答案 0 :(得分:1)

你可以尝试一下这样的正则表达式:

"(\\b[0-9]{3,5},? [A-Za-z]+(?: [A-Za-z]+,?)* [a-zA-Z]{2} [0-9]{5})"

[A-Za-z]+,?部分只允许使用字母(而非数字)。

regex101 demo

答案 1 :(得分:0)

*运算符是贪婪的,因此它匹配尽可能多的字符。在您的表达式中,匹配邮政编码和状态的[a-zA-Z] {2} [0-9] {5}部分与输入中的最后一个ZIP和状态匹配,因为您之前有。*模式在表达式中,尽可能多地扩展为字符。

尝试将.更改为[^0-9],以便它匹配除数字之外的任何内容。