1

I have a req to provide US Zip+4 with the +4 being optional and the +4 can't be 0000. I'm doing this in .NET therefore I'm using RegularExpressionValidator with RegEx set. In my first validator I'm checking if the Zip code is xxxxx-xxxx or xxxxx format that is 5+4 or 5. In my 2nd validator I check if the last 4 are not set to 0000. This means 1234-0000 is invalid. These are my Regex and I want to be sure they are valid. Seems they test okay, however when cross checking them with the regex101 app online I'm getting different behavior than .NET.

xxxxx-xxxx or xxxxx = ^[0-9]{5}(?:-[0-9]{4})?$


xxxxx-0000 = \d{5}(?!-0000).*     

This last one I quite don't understand how it works, but it seems to work. Someone help explain me the ?! and .* they both seem to need to be necessary for this to function. My understanding is the .* means all char and the ?! means negative lookahead????

John
  • 193
  • 1
  • 1
  • 11
  • Does this answer your question? [Regex for string not ending with given suffix](https://stackoverflow.com/questions/16398471/regex-for-string-not-ending-with-given-suffix) – mickmackusa Aug 05 '21 at 06:08

1 Answers1

2

Actually, the regex pattern I would suggest here is actually a combination of the two you provided above:

^[0-9]{5}(?!-0000$)(?:-[0-9]{4})?$

Demo

Here is an explanation of the pattern:

^                   from the start of the ZIP code
    [0-9]{5}        match a 5 digit ZIP code
    (?!-0000$)      then assert that the PO box is NOT -0000
    (?:-[0-9]{4})?  match an optional -xxxx PO box (which can't be 0000)
$                   end of the ZIP code

Of note, the (?!-0000$) term is called a negative lookahead, because it looks ahead in the input and asserts that what follows is not -0000. But, using a lookahead does not advance the pattern, so after completing the negative assertion, the pattern continues trying to match an optional -xxxx PO box following.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks, so the reason why I wanted them to be seperate is because want to provide seperate error messages to the user. One message if it's not in the right format and another error message if they are trying the "0000" on the end. Thanks for the explanation. I haven't done RegEx in a while and based on my research this is kinda what I thought. – John Aug 05 '21 at 04:12
  • In that case, just match separately for `^\d{5}-0000$`, and if it matches, then raise your flag. Otherwise, use your first general pattern. – Tim Biegeleisen Aug 05 '21 at 04:14
  • Thanks, now here's my other issue. There is a case where a user is putting in 12$53-1345 and it's firing the 2nd pattern (the 0000) pattern. How do I get this pattern to only care about the values after the "-" and there must be 5 characters in front, regardless of character type. My first patern ensures they are [0-9]. – John Aug 05 '21 at 04:16
  • Oh, I think I figured it out. I can do ".{5}" which means any 5 characters. – John Aug 05 '21 at 04:19
  • Please close new questions using the canonical. If you want to improve the canonical, please edit one of the answers or post a new answer. https://stackoverflow.com/q/578406/2943403 – mickmackusa Aug 05 '21 at 06:04