-4

I am new to learning Regex and I have tried almost everything myself and from the internet to find a Regex that accepts values from 0 to 65536 and yes I want to do it by Regex only. The Closest I got was 69999.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 2
    _The Closest I got was 69999_ What do you mean by that? What have you tried so far? Why don't you use simple numeric comparison and doing your work more complicate (probably) with regex? – Soner Gönül Apr 20 '15 at 07:45
  • 1
    Regular expressions are generaly good for extracting data, not for some validation if it. So why don't extract that integer and then check if it's in desired bounds? – Andrey Korneyev Apr 20 '15 at 07:49
  • Also, you can google 'Regex generator for number range' and you get [this site](http://utilitymill.com/utility/Regex_For_Range). – Jerry Apr 20 '15 at 07:50
  • I'm actually surprised that web page exists, Jerry... OP, exactly why does it need to use regex? Is this a learning exercise? it doesn't seem like something you should be using regex for.. – Sayse Apr 20 '15 at 07:56

2 Answers2

0

Here it is:

^(?:[0-5]?[0-9]{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6])$

See demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Split it into multiple ranges: 0-9, 10-9999, 10000-59999, 60000-64999, 65000-65499, 65500-65529, 65530-65536

^(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])$

Regular expression visualization

Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31