1

我正在通过将正则表达式与 2-5 位数字或一些邮政信箱内容匹配,然后是 10-100 个字符,然后是州和邮政编码来查找美国地址(粗略搜索)。

我试图将中间匹配限制为包括换行在内的任何字符的 10 到 100 个,同时特别允许其中的空白字符数量不受限制(所有其他字符的总数限制为 100 个)。例如,以下字符串将匹配:

"12345 First St. [hundreds of white space characters]
 Some Town, [hundreds of white space characters]
 CA 92107"

这个正则表达式与我的模式匹配,除了无限的空格。

$regex = '/(.|\n|\r\n){10,100}/';

我试过用这个模式来匹配无限的空白,但它不起作用:

$regex = '/(.|\s+|\n|\r\n){10,100}/';

对于上下文,我用来查找地址的完整正则表达式如下:

$regex = "/\b(\d{2,5}|po|p\.o\.|post office)(.|\n|\r\n){10,100}(AK|Alaska|AL|Alabama|AR|Arkansas|AZ|Arizona|CA|California|CO|Colorado|CT|Connecticut|DC|Washington\sDC|Washington\D\.C\.|DE|Delaware|FL|Florida|GA|Georgia|GU|Guam|HI|Hawaii|IA|Iowa|ID|Idaho|IL|Illinois|IN|Indiana|KS|Kansas|KY|Kentucky|LA|Louisiana|MA|Massachusetts|MD|Maryland|ME|Maine|MI|Michigan|MN|Minnesota|MO|Missouri|MS|Mississippi|MT|Montana|NC|North\sCarolina|ND|North\sDakota|NE|New\sEngland|NH|New\sHampshire|NJ|New\sJersey|NM|New\sMexico|NV|Nevada|NY|New\sYork|OH|Ohio|OK|Oklahoma|OR|Oregon|PA|Pennsylvania|RI|Rhode\sIsland|SC|South\sCarolina|SD|South\sDakota|TN|Tennessee|TX|Texas|UT|Utah|VA|Virginia|VI|Virgin\sIslands|VT|Vermont|WA|Washington|WI|Wisconsin|WV|West\sVirginia|WY|Wyoming)(\s|\n|\r\n|\&nbsp\;){1,3}\d{5}/i"
4

2 回答 2

4

为您的中间匹配尝试以下操作:

\s*(?:\S\s*){10,100}

正则表达式:

\s*            whitespace (\n, \r, \t, \f, and " ") (0 or more times)
 (?:           group, but do not capture (between 10 and 100 times)
  \S           non-whitespace (all but \n, \r, \t, \f, and " ")
  \s*          whitespace (\n, \r, \t, \f, and " ") (0 or more times)
 ){10,100}     end of grouping

所以你可以从你的表达开始,比如..

(\d{2,5}|post office|p[\. ]?o\.?)(\s*(?:\S\s*){10,100})

使用您的数据进行现场演示。

于 2013-09-11T02:03:59.357 回答
0

您可以添加一个锚定的前瞻来断言非空白的总数:

^(?=(\s*\S){10,100)\s*$)\d{1,5}.*[A-Z]{2}\s+\d{5}$

前瞻断言有 10-100 个非空白字符。我还根据您的评论粗略地为实际地址制定了一个基本的正则表达式。

于 2013-09-11T01:54:48.530 回答