分割的街道,门牌号码和地址中的附加内容

问题描述 投票:0回答:2

我正在使用正则表达式从地址中分割荷兰街,门牌号和加法。添加应该是可选的。

preg_match('/(?P<address>[^\d]+) (?P<number>[\d]+)(?P<numberAdd>[^\d]+)/', $input, $matches)

仅在$ input例如是Street 1A时才有效。但如果输入仅是Street 1(不添加),则不是。

那么,如何将地址与附加地址分开作为可选地址?加法不能包含数字。

php regex preg-match
2个回答
0
投票

尝试一下:

preg_match('/(?P<address>\D+) (?P<number>\d+)(?P<numberAdd>\D*)/', $input, $matches)

4
投票

您可以使用一个匹配很多的正则表达式。在this Gist中进行了描述。该代码也提供德语地址。

var re = /^(\d*[\wäöüß\d '\-\.]+)[,\s]+(\d+)\s*([\wäöüß\d\-\/]*)$/i;

var adressen = [
  'Dorpstraat 2',
  'Dorpstr. 2',
  'Laan 1933 2',
  '18 Septemberplein 12',
  'Kerkstraat 42-f3',
  'Kerk straat 2b',
  '42nd street, 1337a',
  '1e Constantijn Huigensstraat 9b',
  'Maas-Waalweg 15',
  'De Dompelaar 1 B',
  'Kümmersbrucker Straße 2',
  'Friedrichstädter Straße 42-46',
  'Höhenstraße 5A',  
  'Saturnusstraat 60-75',
  'Saturnusstraat 60 - 75',
  '1, rue de l\'eglise'
], match;

adressen.forEach(function(adres) {
  match = adres.match(re)
  if (match) {
    match.shift(); // remove element 0 (the entire match)
    //match is now always an array with length of 3
    console.log(match.join('|'))
  } else {
    console.log('No match: '+adres)
  }
})

这是格式化的输出

Dorpstraat                   |    2 | 
Dorpstr.                     |    2 | 
Laan 1933                    |    2 | 
18 Septemberplein            |   12 | 
Kerkstraat                   |   42 | -f3 
Kerk straat                  |    2 | b 
42nd street                  | 1337 | a 
1e Constantijn Huigensstraat |    9 | b
Maas-Waalweg                 |   15 | 
De Dompelaar                 |    1 | B
Kümmersbrucker Straße        |    2 |
Friedrichstädter Straße      |   42 |-46
Höhenstraße                  |    5 | A
Saturnusstraat               |   60 | -75
Saturnusstraat 60 -          |   75 | //<-- problematic

No match: 1, rue de l'eglise

非常欢迎您提供regexp改进,但不要忘记查看它是否仍与它用来匹配的所有内容匹配。

© www.soinside.com 2019 - 2024. All rights reserved.