正则表达式分裂荷兰地址

时间:2016-03-11 13:58:35

标签: php regex

我试图拆分荷兰语地址,但我有点卡住了。

问题是荷兰街道上可能有数字。

$input_string = "name3 street 24a";

$address = "";
$number = "";
$addition = "";

$matches = array();
if(preg_match('/(?P<address>[^\d]+) (?P<number>\d+.?)/', $input_string, $matches)){
    $address = $matches['address'];
    $number = $matches['number'];
    if (preg_match('/(?P<nr>[^\d]+) (?P<addition>\d+.?)/', $number, $matches)) {
        $number = $matches['nr'];
        $addition = $matches['addition'];
    }
} else { // no number found, it is only address
    $address = $input_string;
}
echo $address . "\n";
echo $number. "\n";
echo $addition. "\n";

将忽略&#34; name3&#34;。

我想我试图去除最后的数字和一个可选的字母。 任何人都可以帮我解决这个问题吗?

示例输入:

  

name3 street 22a

     

街3

     街道3a

     

博士。街24a

1 个答案:

答案 0 :(得分:0)

  

我想我正试图去掉最后的数字和一封可选的字母。

这可以通过跟随正则表达式完成。

正则表达式: (?:\d+[a-z]*)$

说明:它会查找number后跟可选的letter

替换为:替换为nothing

Regex101 Demo