Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python regex to match street name and house number

I have a regex which matches street names (see demo 1) and I have a regex which matches possible house numbers in Germany (see demo 2). Each regex works perfectly fine. In the next step I want to combine both regex (street names + house number). In other words, I am looking for a regex which matches both street names and house numbers together.

I have prepared a demo 3 with examples. I know these examples are not complete if you compare it with the strict rules here, but it is enough for my use case.

Since regex is a rule based language, let me try to explain the rules in words:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • In Germany the street name can be basicaly every kind of name. There can be a . or - in between.
  • The regex should match even with lower case characters
  • The house numbers are in most cases sth. like 99 or 99a. But I tried to be creative and added some additional possible examples

My problem:
I have solutions to each seperate case (see demo 1 and 2) but my problem is that I don’t know how to combine two regex to one (see demo 3).

Working regex for street names:

^(?:[A-Z] \d|[^\W\d_]{2,}\.?)(?:[- '’][^\W\d_]+\.?)*$

Working regex for house numbers:

^[1-9]\d{0,3} ?[a-zA-Z]?(?: ?[/-] ?[1-9]\d{0,3} ?[a-zA-Z]?)?$

Based on the regex shown above, how do I combine both of them in order to match my examples shown in demo 3?

>Solution :

Combining 2 regex into one:

^(?:[A-Z] \d|[^\W\d_]{2,}\.?)(?:[- '’][^\W\d_]+\.?)*\s+[1-9]\d{0,3} ?[a-zA-Z]?(?: ?[/-] ?[1-9]\d{0,3} ?[a-zA-Z]?)?$

Updated RegEx Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading