Java正则表达式异常



我有一个地址列表,如下所示:

Find Dabla Inc at 6889 66th Ave, NY, NY 11884-3773. Call them at (888) 292-1655.
Find Walgreen Drug Stores at 6008 87th Ave, NY, NY 17774-4314. Call them at (888) 999-473
Find Silver Star Restaurant at 6941 99th Ave Ste A, NY, NY 88804-2915. Call them at (888) 851-2799.

我正试图通过Regex从这个文本中提取街道地址,州,城市和邮政编码。

目前,我使用以下方式匹配街道地址:

    (?<=ats)d{3,5}sS*sw*.*(?=,)

然而,我看到它包含在match NY中,而不是在第一个逗号之后停止(它只在第二个逗号之后停止)。如果我用的是正向前看为什么要包括第一个逗号呢?

Thannks

(?<=ats)d{3,5}sS*sw*.*?(?=,)
                            ^^

让你的正则表达式non greedy .

参见demo: https://regex101.com/r/fX3oF6/14

最新更新