I'm using this regex (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3} to validate port numbers. Somehow this is not working. What is wrong with this? Can anybody point me out.
-
What are you trying to do? Maybe post some unit test code that is failing or something?Bob Kuhar– Bob Kuhar2012-10-19 05:51:37 +00:00Commented Oct 19, 2012 at 5:51
-
what port numbers do you want 2 validate..are they in specific range or do u want it 2 just validateAnirudha– Anirudha2012-10-19 06:24:41 +00:00Commented Oct 19, 2012 at 6:24
-
@Fake.It.Til.U.Make.It I just want to validate from 1 to 65535.Soham Dasgupta– Soham Dasgupta2012-10-26 11:03:36 +00:00Commented Oct 26, 2012 at 11:03
-
2@SohamDasgupta no need to use regex here..just parse the number to int and then check if it is in required range!Anirudha– Anirudha2012-10-26 12:58:57 +00:00Commented Oct 26, 2012 at 12:58
13 Answers
What exactly do you mean by not working?
You could try something like so: ^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$ (obtained from here).
This will make sure that any given string is numeric and between the range of 0 and 65535.
Assuming your regular expression matches the same range, it is missing the start and end anchors (^ and $ respectively), so it would allow other strings besides the actual port.
Update 2 Feb 2022: Fixed the regex to reject values like 00 etc. The updated regex is sourced from the comment below. This regex can be better understood and visualized here: https://www.debuggex.com/r/jjEFZZQ34aPvCBMA
5 Comments
[0-9]{1,4}) is incorrectly allowing 0000. It should be [1-9]\\d{0,3}|0When, we search "how to validate port number" on Google we unfortunately land here
However (except if you have really no other choice...),
Regex is clearly not the way to validate a port number !
"One" (slightly better) way may be:
step 1: Convert your string into number, and return FALSE if it fails
step 2: return TRUE if your number is in [1-65535] range, and FALSE otherwise
Various reasons, why Regex is not the right way ?
- Code readability (would takes few minutes to understand)
- Code robustness (there are various ways to introduce a typo, a unitary test would be required)
- Code flexibility (what if port number can be extended to a 64-bits number !?)
- etc. ...
1 Comment
System.UInt16.Tryparse(string value, out int result). Although UInt16 range includes zero as well which is not a valid port number.Here is the example I'm using to validate port settings for a firewall. The original answer will match 2 strings. I can only have 1 string match.
(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[1-9](\d){0,3})
To get: 22,24:100,333,678,100:65535 my full validation (That will only return 1 match) is
(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3})(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}))?(,(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}){1}(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}))?)*
Comments
Number() is the function you want "123a" returns NAN
parseInt() truncates trailing letters "123a" returns 123
<input type="text" id="txtFld" onblur="if(Number(this.value)>0 && Number(this.value)<65536){alert('valid port number');}" />
1 Comment
A more strict approach is to have a regex matching all numbers up to 5 digits with the following string:
*(^[1-9]{1}$|^[0-9]{2,4}$|^[0-9]{3,4}$|^[1-5]{1}[0-9]{1}[0-9]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-4]{1}[0-9]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-5]{1}[0-4]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-5]{1}[0-5]{1}[0-3]{1}[0-5]{1}$)*
Comments
Here is single port regex validation that excludes ports that start with 0
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])
Here is validation for port range (ex. 1111-1111)
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])(-([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$
link:
Comments
Landed here as well, searching specifically for REGEX to validate port number. I see the approved solution was not fixed yet to cover all scenarios ( eg: 007 port, and others ) and solutions from other sites not updated either (eg).
Reached same minimal solution as saber tabatabaee yazdi, that should cover the 1-65535 range properly:
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
Enjoy !
Comments
^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0][0-9]{1,4})|([0-9]{1,4}))$
I have tested above regrex with Junit run the for loop from 0-65535
Ex: 00001 - 65535 with leading Zeros 1 - 65535 without leading Zeros Ex:====
(6553[0-5]): 65530-65535(655[0-2][0-9]): 65500-65529(65[0-4][0-9]{2}): 65000-65499(6[0-4][0-9]{3}): 60000-64999([1-5][0-9]{4}): 10000-59999([0-5]{0,5}): 00000-55555 (for leading Zeros)([0][0-9]{1,4}): 00000-09999 (for leading Zeros)([0-9]{1,4}): 0000-9999 (for leading Zeros)
Comments
@npinti 's answer allows leading zeros in the port number and also port 0 means pick any available port so I would exclude that so the regex becomes
^([1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
If you want to allow port 0 then
^(0|[1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
2 Comments
[1-9][0-9]{0,4}, but this means numbers like 99999 can get in, which violates the question. I'll keep trying to see if I can find a correction for this.[1-9][0-9]{0,3} instead? This will cover 1-9999 inclusive, but disallow leading zeroes.