I’m using this code
(\d{5})([\-]\d{4})?\s*
Which matches my Zip Code
12345-1234
in 2 groups
1. 12345
2. -1234
I need it to match both 12345 (5 char zip) and 12345-1234 (10 char zip) in 1 single group. Is that possible?
EDIT:
(\d{5}-\d{4}|\d{5})
(Thanks to vcsjones) satisfies the original question.
However it doesn’t retrieve only 12345 in case of a malformed Zip (eg. 12345-123)
Is there any way to do that?
You can give this one a try:
Edit:
Are you sure? This produces a match: