且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在文本中查找美国街道地址(***使用 Python 正则表达式)

更新时间:2023-09-02 16:33:16

\d{1,4}( \w+){1,5}, (.*), ( \w+){1,5}, (AZ|CA|CO|NH), [0-9]{5}(-[0-9]{4})?

在这个正则表达式中,你有一个太多的空格(在 ( \w+){1,5} 之前,它已经以 1 开头).删除它,它与您的示例相匹配.

我认为您不能假设单元 123"或类似单元会在那里,或者可能有多个单元(例如建筑 A,apt 3").请注意,在您的初始正则表达式中, . 可能与 , 匹配,这可能会导致很长(和不需要的)匹配.您可能应该接受几个这样的组,但有数量限制(例如,将 , (.*) 替换为 (, [^,]{1,20}){0,5}.

无论如何,您可能永远不会得到 100% 准确的东西,它会接受人们可能扔给他们的任何变化.做大量的测试!祝你好运.

Disclaimer: I read very carefully this thread: Street Address search in a string - Python or Ruby and many other resources.

Nothing works for me so far.

In some more details here is what I am looking for is:

The rules are relaxed and I definitely am not asking for a perfect code that covers all cases; just a few simple basic ones with assumptions that the address should be in the format:

a) Street number (1...N digits);

b) Street name : one or more words capitalized;

b-2) (optional) would be best if it could be prefixed with abbrev. "S.", "N.", "E.", "W."

c) (optional) unit/apartment/etc can be any (incl. empty) number of arbitrary characters

d) Street "type": one of ("st.", "ave.", "way");

e) City name : 1 or more Capitalized words;

f) (optional) state abbreviation (2 letters)

g) (optional) zip which is any 5 digits.

None of the above needs to be a valid thing (e.g. an existing city or zip).

I am trying expressions like these so far:

pat = re.compile(r'\d{1,4}( \w+){1,5}, (.*), ( \w+){1,5}, (AZ|CA|CO|NH), [0-9]{5}(-[0-9]{4})?', re.IGNORECASE)

>>> pat.search("123 East Virginia avenue, unit 123, San Ramondo, CA, 94444")

Don't work, and for me it's not easy to understand why. Specifically: how do I separate in my pattern a group of any words from one of specific words that should follow, like state abbrev. or street "type ("st., ave.)?

Anyhow: here is an example of what I am hoping to get: Given def ex_addr(text): # does the re magic # returns 1st address (all addresses?) or None if nothing found

for t in [
'The meeting will be held at 22 West Westin st., South Carolina, 12345 on Nov.-18',
'The meeting will be held at 22 West Westin street, SC, 12345 on Nov.-18',

'Hi there,\n How about meeting tomorr. @10am-sh in Chadds @ 123 S. Vancouver ave. in Ottawa? \nThanks!!!',
'Hi there,\n How about meeting tomorr. @10am-sh in Chadds @ 123 S. Vancouver avenue in Ottawa? \nThanks!!!',

'This was written in 1999 in Montreal',

"Cool cafe at 420 Funny Lane, Cupertino CA is way too cool",

"We're at a party at 12321 Mammoth Lane, Lexington MA 77777; Come have a beer!"
] print ex_addr(t)

I would like to get:

'22 West Westin st., South Carolina, 12345'
'22 West Westin street, SC, 12345'
'123 S. Vancouver ave. in Ottawa'
'123 S. Vancouver avenue in Ottawa'
None # for 'This was written in 1999 in Montreal',
"420 Funny Lane, Cupertino CA",
"12321 Mammoth Lane, Lexington MA 77777"

Could you please help?

\d{1,4}( \w+){1,5}, (.*), ( \w+){1,5}, (AZ|CA|CO|NH), [0-9]{5}(-[0-9]{4})?

In this regex, you have one too many spaces (before ( \w+){1,5}, which already begins with one). Removing it, it matches your example.

I don't think you can assume that a "unit 123" or similar will be there, or there might be several ones (e.g. "building A, apt 3"). Note that in your initial regex, the . might match , which could lead to very long (and unwanted) matches. You should probably accept several such groups with a limitation on the number (e.g. replace , (.*) with something like (, [^,]{1,20}){0,5}.

In any case, you will probably never get something 100% accurate that will accept any variation people might throw at them. Do lots of tests! Good luck.