Can someone explain this regex, especially what "(19|20)?" does in ^(19|20)?[0-9]{6}[- ]?[0-9]{4}$

Viewed 26

^(19|20)?[0-9]{6}[- ]?[0-9]{4}$

  1. I know that ^ and $ tells us the beginning and end of the string.
  2. [0-9]{6} and [0-9]{4} tells us to have numbers from 0-9 six times and four times.
  3. [- ]? tells us that we can have "-" between them. But why do we have space between - and ].

and to the question that I'm wondering the must, what does "(19|20)?" do. I know that | is an or, so 19 or 20. But I can still use another number like 60 or whatever at the beginning without getting any error. What is the benefit of (19|20)? :)

0 Answers
Related