Search code examples
pythonregexstreet-address

How to split a Swiss address into a street and a zip code in Python?


I am looking for a solution to split a representative Swiss address into a street (and street number) and a zip code (and name of place).

Suggest, I have the following address:

'Bahnhofstrasse 1, 8001 Zürich'

The result I am looking for is:

street: 'Bahnhofstrasse 1'
place: '8001 Zürich'

However, sometimes there is a comma and sometimes not. But the postal code always consists of 4 digits?

I used the .split(') so far but that only works when a comma is present.


Solution

  • I don't expect city names to have digits in them, Use this Pattern ^(.*?),?\s*(\d{4}\D+)$ Demo

    ^               # Start of string/line
    (               # Capturing Group (1)
      .             # Any character except line break
      *?            # (zero or more)(lazy)
    )               # End of Capturing Group (1)
    ,               # ","
    ?               # (zero or one)(greedy)
    \s              # <whitespace character>
    *               # (zero or more)(greedy)
    (               # Capturing Group (2)
      \d            # <digit 0-9>
      {4}           # (repeated {4} times)
      \D            # <character that is not a digit>
      +             # (one or more)(greedy)
    )               # End of Capturing Group (2)
    $               # End of string/line