Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for 2 numbers of the same length separated by dash

What's the right regex for matching the following criteria:

  • Two numbers, separated by a dash (-)
  • Both numbers must have the same amount of digits, but at least 1 and not more than 5 digits
  • before/after/between numbers and the dash there may be arbitrary whitespace

Examples that match:

  • 123-444
  • 1234 - 5678
  • 98- 76

Examples that shouldn't match:

  • 1234-567
  • 123456-789012

Is such a thing possible?

like image 979
Askaga Avatar asked Dec 12 '22 08:12

Askaga


1 Answers

Probably you can use this:

^ *((\d *- *\d)|(\d{2} *- *\d{2})|(\d{3} *- *\d{3})|(\d{4} *- *\d{4})|(\d{5} *- *\d{5})) *$

Online Demo: http://regex101.com/r/jG0dB7

like image 184
anubhava Avatar answered Dec 30 '22 00:12

anubhava