I am currently working on a project where the current Regular Expression being used for Zip Codes is "\d{5}(-\d{4})?"
and the following test cases pass.
- 12345-1000
- 12345
- 123451231
- 12345-123
- 12345adfe
However, when I use the Regular Expression "^[0-9]{5}(?:-[0-9]{4})?$"
, only the following test cases pass
- 12345
- 12345-1234
The above is the correct behavior as following Zip codes standards.
- Clearly the issue is solved, but I was wondering if someone could explain why this is the case?
- Am I misunderstanding how "\d" works when I say that it uses digits 0-9?
- What am I doing differently that FluentValidation uses the first regex differently than the Data Annotations do?