Milan Tripathi Milan Tripathi - 1 month ago 8
C# Question

Validate an integer with a specific 0-65536 range in Regex C#

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.

Answer

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