1

I have written this regex but this is not working as expected.

((?:[A-Z][\w]+)?),[\s]([A-Z]{2})

Following are sample inputs.

Fort Worth, TX
This is Forth Worth, TX
We are looking for someone from Columbus, MS.

I expect City and State out of above string samples with regex in python but this is just no working as expected.

print re.findall('((?:[A-Z][\w]+){1,2}),[\s]([A-Z]{2})', input)

What am I missing?

4

2 回答 2

1

为什么不只是:

([A-Z][^,]+),\s([A-Z]{2})

或者,如果城市名称中只能包含单词字符和空格,则:

([A-Z][\w\s]+),\s([A-Z]{2})

正则表达式 101 演示

于 2013-09-16T18:09:18.520 回答
1

您可能想在您的城市名称组中添加一个空格字符,如下所示:

re.findall('((?:[A-Z][\w]+\s*){1,2}),[\s]([A-Z]{2})', input)

这将匹配一个大写拉丁字母,后跟一个或多个单词字符和零个或多个空格字符,所有这些都可能出现一两次,在第 1 组中捕获,后跟一个逗号、一个空格字符和两个大写拉丁字母,在第 2 组中捕获。

于 2013-09-16T18:09:59.403 回答