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 KuharCommented 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 validate– AnirudhaCommented Oct 19, 2012 at 6:24
-
@Fake.It.Til.U.Make.It I just want to validate from 1 to 65535.– Soham DasguptaCommented 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!– AnirudhaCommented 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
-
I'm using DexExpress textbox and using this regex as mask. But at runtime its generating an error saying incorrect syntax. Commented Oct 19, 2012 at 5:57
-
-
Putting qoutes is something I learned 10yrs back when I started programming. Anyway thanks. Commented Oct 19, 2012 at 6:12
-
8The first part (
[0-9]{1,4}
) is incorrectly allowing0000
. It should be[1-9]\\d{0,3}|0
– elfanCommented Mar 28, 2017 at 9:17 -
2Hi, @elfan that accept 00 or 000 or 0000 I find accidentally this entry and realized there is a little mistake in the pattern: "After correction of your regexp : Port = '([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-3][0-9]|6553[0-5])'; // 1..65535" This Pattern is allowed for the range between 1-65539, it should be: Port = '([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..65535 github.com/findhit/proxywrap/issues/13 Commented Jul 12, 2018 at 11:50
When, 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. ...
-
Good suggestion. For step 1,we can use
System.UInt16.Tryparse(string value, out int result)
. AlthoughUInt16
range includes zero as well which is not a valid port number.– RBTCommented Jul 7, 2021 at 11:38
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}))?)*
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');}" />
-
Too late :) , but this is obviously wrong. You can test it with "2e2" for example. Commented Oct 23, 2019 at 7:00
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}$)*
"^((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-9]{1,4}))$"
It will allow everything between 0-65535 inclusive.
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:
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 !
^((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)
The accpeted answer by npinti is not right. It will not allow to enter port number 1000, for example. For me, this one (not nice, I'm a beginner) works correctly:
/^((((([1-9])|([1-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9][0-9][0-9])|([1-6][0-5][0-5][0-3][0-5])))))$/
@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])$
-
I really like the intent of this answer as I have the exact same problem right now. Notice that your main difference is that the first capture group allows
[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. Commented Oct 13, 2016 at 19:22 -
1I think you meant to use
[1-9][0-9]{0,3}
instead? This will cover 1-9999 inclusive, but disallow leading zeroes. Commented Oct 13, 2016 at 19:25
The solution:
Dim Minlenght As Integer = 1
Dim Maxlenght As Integer = 65536
Regex.IsMatch(sInput,"(^\d{0},{1}$)", "{" + Minlenght, Maxlenght + "}")
If variable is an integer between 1 and 65536 (inclusive) then...
if [[ "$port" =~ ^[0-9]+$ && $port -ge 1 && $port -le 65536 ]]; then