How to add one more pattern to my street-name regex?

Viewed 101

I already have a working example of a regex that matches most German street names. However, I recently learned, that the following patterns are possible as well:

  • A 1
  • K 7
  • H 2
  • P 1
  • I 7
  • E 7
  • T 3
  • Z 9

How can I build this pattern into my existing Python-regex?:

^(?:[^\W\d_]+\.?|\([^\W\d_]+\.?\))(?:[- '’](?:[^\W\d_]+\.?|\([^\W\d_]+\.?\)))*$
2 Answers

You can just add it with an |. It also seems that your pattern can be heavily simplified:

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

https://regex101.com/r/WRd1jx/7

You could add matching an uppercase char followed by a space and 1+ digits to the alternation.

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

Regex demo

Related