Bahamas
Bahamas

Reputation: 397

How to regex German street addresses with numbers (infix)

I've got these two addresses:

Straße des 17 Juni 122a
Str. 545 3

See https://regex101.com/r/2WT48R/5

I need to filter for the street and number.

My desired output would be:

streets = [Straße des, Str. ]
numbers = [17 Juni 122a, 545 3]

This is my regex:

(?<street>[\S ]+?)\s*(?<number>\d+[\w\s\/-]*)$

Output should look like:

streets = [Straße des 17 Juni, Str. 545]
numbers = [122a, 3]

Upvotes: 1

Views: 798

Answers (2)

Bahamas
Bahamas

Reputation: 397

I found one answer by myself:

(?<street>[\S ]+?)\s*(?<number>\d+\s*[a-zA-Z]*\s*([-\/]\s*\d*\s*\w?\s*)*)$

The demo includes several additional test cases.

Upvotes: 1

Nick Reed
Nick Reed

Reputation: 5059

Looks like there's no spaces in the "numbers" part of your regex - you can use that to cut away those extra characters getting stuck in your second capture group.

(?<street>[\S ]+)\s(?<number>\d+\S*$)

By allowing no whitespace in the second capture group, it won't match the numbers 17 or 545 too early.

Demo

EDIT: after seeing your more detailed list of examples on your own demo, the following regex will match the complete set of your test cases:

(?<street>[\S \t]+?) ?(?<number>[\d\s]+[\w-\/]*?$)

Demo

Upvotes: 2

Related Questions