Regex for splitting a german address into its parts

前端 未结 6 750
孤街浪徒
孤街浪徒 2021-02-09 18:09

Good evening,

I\'m trying to splitting the parts of a german address string into its parts via Java. Does anyone know a regex or a library to do this? To split it like t

6条回答
  •  无人共我
    2021-02-09 18:25

    I came across a similar problem and tweaked the solutions provided here a little bit and came to this solution which also works but (imo) is a little bit simpler to understand and to extend:

    /^([a-zäöüß\s\d.,-]+?)\s*([\d\s]+(?:\s?[-|+/]\s?\d+)?\s*[a-z]?)?\s*(\d{5})\s*(.+)?$/i
    

    Here are some example matches.

    It can also handle missing street numbers and is easily extensible by adding special characters to the character classes.

    [a-zäöüß\s\d,.-]+?                         # Street name (lazy)
    [\d\s]+(?:\s?[-|+/]\s?\d+)?\s*[a-z]?)?     # Street number (optional)
    

    After that, there has to be the zip code, which is the only part that is absolutely necessary because it's the only constant part. Everything after the zipcode is considered as the city name.

提交回复
热议问题