PParker
PParker

Reputation: 1511

Regex for German house number

Looking for a regex that specifies a German house number:

Valid house number:

Not valid house number:

The last example excludes every non Latin character

My regex doesn't match the way it should:

\d+(\s|-)?\w*$

EDIT: Sorry, I had to change the examples above! Please have a look!

Upvotes: 3

Views: 3025

Answers (3)

JvdV
JvdV

Reputation: 75840

Making the assumption (since you haven't provided those) this should also exclude patterns like 12-a1, 12-1a, 12-01 and 12-A1. If so, then maybe:

^[1-9]\d*(?:[ -]?(?:[a-zA-Z]+|[1-9]\d*))?$

See the Online Demo


  • ^ - Start string ancor.
  • [1-9]\d* - A single digit from range 1-9 followed by zero or more digits.
  • (?: - 1st Non-capture group.
    • [ -]? - An optional space or hyphen.
    • (?: - 2nd Non-capture group.
      • [a-zA-Z]+|[1-9]\d* - One or more characters from a-zA-Z or single digit from range 1-9 followed by zero or more digits.
    • ) - Closing 2nd non-capture group
  • ) - Closing 1st non-capture group
  • ? - Optional 1st non-capture group (to allow for single digits numbers).
  • $ - End string ancor.

Regular expression visualization

Upvotes: 5

The fourth bird
The fourth bird

Reputation: 163277

You could make the space or hyphen optional [-\s]? and make 1+ word chars together in an optional group.

If the housenumber can not start with a zero, you could start the match with [1-9]

^[1-9]\d*(?:[-\s]?\w+)?$

Regex demo

Matching only a-z or 0-9 or A-Z if 25 A is also valid

^[1-9]\d*(?:[-\s]?[a-zA-Z0-9]+)?$

Regex demo

Upvotes: 4

pmdj
pmdj

Reputation: 23428

First there's the question of whether this is a good idea or not… (see various articles titled "Falsehoods programmers believe about XYZ") I'm not in Germany but nearby Austria, and my address does not match any of your examples. (There are 2 ways to write it, and neither matches one of yours.)

Aside from that:

  1. What matches are you expecting that don't match?
  2. What doesn't match which you'd expect?
  3. What regex dialect are you using?

Some obvious issues:

\d+(\s|-)?\w*$
   ^^^^^^^^^^

* matches 0 instances. So you're allowing a dash or space followed by no characters. Probably not what you wanted. Try:

\d+((\s|-)?\w+)?

This will make the whole letter block optional, but it must always have a letter.

Speaking of which, letters \w are not digits. So you'll need to explicitly allow them.

\d+ will match 1 or more digits, not "a number not prefixed with 0". If your first digit must not be zero, you'll need to be explicit about this: [1-9]\d*.

You have an end matching character $ but no start matching ^. You probably want either none or both, depending on whether your regex check is of the "match" or "search" kind. (i.e. if your regex library call attempts to validate the entire input string, or whether any substring matches.)

Upvotes: 0

Related Questions