如何使用Ruby中的正则表达式从文本中提取地址

我试图从文本中提取美国地址。

因此,如果我有以下文本变体,那么我想提取地址部分

今天是在酒吧见面的好日子。 地址是123 fake street,NY,23423-3423

just came from 423 Elm Street, kk, 34223 ...had awesome time 

blah blah bleh blah 23414假露台,MM别的东西

  experimented my teleporter to get to work but reached at 2423 terrace NY 

如果有人可以提供一些起点,那么我可以根据其他变化进行塑造。

在某些时候,你已经澄清了你认为的地址。

地址是否只有街道号码和街道名称?

地址是否有街道名称和城市名称?

地址是否有城市名称,州名?

地址是否包含城市名称,州名缩写和邮政编码? 邮政编码是什么格式的?

很容易看出你如何快速遇到麻烦。

这显然不会捕捉到所有内容,但也许你可以匹配以街道号码开头的字符串,在中间某处有一个州名缩写,并以邮政编码结尾。 这种可靠性很大程度上取决于你知道你使用什么类型的文本作为输入。 即,如果文本中有很多其他数字,这可能完全没用。

可能的正则表达式

 \d+.+(?=AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY)[AZ]{2}[, ]+\d{5}(?:-\d{4})? 

样本输入

 hello world this is me posting an address. please go to 312 N whatever st., New York NY 10001. If you can find me there. I might be at 123 Invalid address. Please send all letters to 115A Address Street, Suite 100, Google KS, 66601 42 NE Another Address, Some City with 9 digit zip, AK 55555-2143 Hope this helps! 

火柴

 312 N whatever st., New York NY 10001 115A Address Street, Suite 100, Google KS, 66601 42 NE Another Address, Some City with 9 digit zip, AK 55555-2143 

正则表达式解释

 \d+ digits (0-9) (1 or more times (matching the most amount possible)) .+ any character except \n (1 or more times (matching the most amount possible)) (?= look ahead to see if there is: AL|AK|AS|... 'AL', 'AK', 'AS', ... (valid state abbreviations) ) end of look-ahead [AZ]{2} any character of: 'A' to 'Z' (2 times) [, ]+ any character of: ',', ' ' (1 or more times (matching the most amount possible)) \d{5} digits (0-9) (5 times) (?: group, but do not capture (optional (matching the most amount possible)): - '-' \d{4} digits (0-9) (4 times) )? end of grouping