Reputation: 13
I am struggeling to find the right regex to match with certain house number formats:
Following formats shall be found:
1 / 1a / 1a-z
What should not match is something like: 1 Building 1
So far I got '^\d[A-Za-z]?[-]?[A-Za-z]?$'
which works for all cases but also matches 1ab.
So how can I add the requirement of having only 1 letter after the digit?
Upvotes: 0
Views: 237
Reputation: 156
I believe the regular expression you are aiming for is something along the lines of ^\d+([A-Za-z](-[A-Za-z])?)?$
I'll break this down:
\d
represents any digit+
means 1 or more of (so \d+
matches 1, 12, 123, etc.)(
and )
contain a group?
means zero or one of[A-Za-z]
matches any character in the ranges A-Z
or a-z
[A-Za-z](-[A-Za-z])?
means a letter, optionally followed by a hyphen and another letterFor a visualisation, see here (tool not mine)
Upvotes: 1