Reputation: 569
Why does this regex not match 3a
?
(\/\d{1,4}?|\d{1,4}?|\d{1,4}[A-z]{1})
Using \d{1,4}\D{1}
, the result is the same.
Streets numbers:
/1
78
3a
89/
-1 (special case)
1
https://regex101.com/r/cYCafR/3
Upvotes: 1
Views: 138
Reputation: 626747
The digits+letter combination is not matched due to the order of alternatives in your pattern. The \d{1,4}?
matches the digit before the letter, and \d{1,4}[A-z]{1}
does not even have a chance to step in. See the Remember That The Regex Engine Is Eager article.
The \/\d{1,4}?
will match a /
and a single digit after the slash, and \d{1,4}?
will always match a single digit, as {min,max}?
is a lazy range/interval/limiting quantifier and as such only matches as few chars as possible. See Laziness Instead of Greediness.
Besides, [A-z]
is a typo, it should be [A-Za-z]
.
It seems you want
\d{1,4}[A-Za-z]|\/?\d{1,4}
See the regex demo. If it should be at the start of a line, use
^(?:\d{1,4}[A-Za-z]|\/?\d{1,4})
See this regex demo.
Details
^
- start of a line(?:
- start of a non-capturing group
\d{1,4}[A-Za-z]
- 1 to 4 digits and an ASCII letter|
- or\/?
- an optional /
\d{1,4}
- 1 to 4 digits)
- end of the group.Upvotes: 3
Reputation: 271050
Your regex uses lazy quantifiers like {1,4}?
. These will match one character, and stop, because the rest of the pattern (i.e. nothing) matches the rest of the string. See here for how greedy vs lazy quantifiers work.
Another reason is that you put the \d{1,4}[A-z]{1}
case last. This case will only be tried if the first two cases don't match. With 3a
, the 3
already matches the second case, so the last case won't be considered.
You seem to just want:
^(\d{1,4}[A-Za-z]|\/?\d{1,4})
Note how the \/\d{1,4}
case and the \d{1,4}
case in your original regex are combined into one case \/?\d{1,4}
.
Upvotes: 1