0

I'm trying to write a regex that follows these constraints:

  • Exactly 5 digits
  • Sometimes, but not always, followed by a dash with 4 more digits
  • Zip code needs to be preceded by at least one whitespace
  • Cannot be at the start of a text

I've arrived at this but it's not giving me the output I want:

r"^[A-Za-z].*\s.*\d{5}(?:[-\s]\d{4})?$"
inkblot
  • 113
  • 3
  • 4
  • 10
  • Can you provide your input and expected output? – Phix Jul 28 '20 at 00:54
  • I'm not sure what you're trying to accomplish with A-Za-z. You certainly did not mention anything about that in your requirements. Make sure you really understand the Regex language well. – JoelFan Jul 28 '20 at 00:57
  • Why do you start with `[A-Za-z]`? Did you look at [`re` docs](https://docs.python.org/3/library/re.html)? – zvone Jul 28 '20 at 00:58
  • 1
    In general to troubleshoot a regex, take stuff out of it until it starts matching. Then gradually add stuff to it. That way you know exactly what problem to focus on. – JoelFan Jul 28 '20 at 00:59
  • My thinking was that [A-Za-z] would mean that the ZIP code cannot be at the start of the sentence. – inkblot Jul 28 '20 at 22:24

1 Answers1

1

I would use:

(?<=[ \t])((?:\d{5}(?=[^\d-]|$))|(?:\d{5}-\d{4}(?=[^\d-]|$)))

Demo and explanation

dawg
  • 98,345
  • 23
  • 131
  • 206