Search code examples
phpregexpreg-match

Split street, house number and addition from address


I'm using a regular expression to split Dutch street, house number and addition from an address. The addition should be optional.

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

This only works if the $input is for example Street 1A. But not if the input is only Street 1 (without addition).

So how can I split the address with the addition as optional? The addition may not contain numbers.


Solution

  • Have a try with:

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