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.

- 5,061
- 24
- 79
- 125
-
What are you trying to do? Maybe post some unit test code that is failing or something? – Bob Kuhar Oct 19 '12 at 05:51
-
what port numbers do you want 2 validate..are they in specific range or do u want it 2 just validate – Anirudha Oct 19 '12 at 06:24
-
@Fake.It.Til.U.Make.It I just want to validate from 1 to 65535. – Soham Dasgupta Oct 26 '12 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 Oct 26 '12 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

- 4,182
- 1
- 30
- 45

- 51,780
- 5
- 72
- 96
-
I'm using DexExpress textbox and using this regex as mask. But at runtime its generating an error saying incorrect syntax. – Soham Dasgupta Oct 19 '12 at 05:57
-
@SohamDasgupta: Maybe you need to put everything between quotation marks? Maybe looking at [the](http://documentation.devexpress.com/#WindowsForms/CustomDocument1501) documentation will help. – npinti Oct 19 '12 at 06:10
-
Putting qoutes is something I learned 10yrs back when I started programming. Anyway thanks. – Soham Dasgupta Oct 19 '12 at 06:12
-
8The first part (`[0-9]{1,4}`) is incorrectly allowing `0000`. It should be `[1-9]\\d{0,3}|0` – elfan Mar 28 '17 at 09: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 https://github.com/findhit/proxywrap/issues/13 – saber tabatabaee yazdi Jul 12 '18 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. ...

- 469
- 6
- 9
-
Good suggestion. For step 1,we can use `System.UInt16.Tryparse(string value, out int result)`. Although `UInt16` range includes zero as well which is not a valid port number. – RBT Jul 07 '21 at 11:38
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');}" />

- 822
- 8
- 16
-
Too late :) , but this is obviously wrong. You can test it with "2e2" for example. – Mohsen Kamrani Oct 23 '19 at 07:00
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}))?)*

- 305
- 1
- 10
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}$)*
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:

- 4,404
- 3
- 42
- 58

- 23
- 6
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 !

- 21
- 2
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])))))$/

- 2,993
- 1
- 29
- 42

- 37
- 9
@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])$

- 1,951
- 3
- 27
- 44
-
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. – daniel.caspers Oct 13 '16 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. – daniel.caspers Oct 13 '16 at 19:25
The solution:
Dim Minlenght As Integer = 1
Dim Maxlenght As Integer = 65536
Regex.IsMatch(sInput,"(^\d{0},{1}$)", "{" + Minlenght, Maxlenght + "}")

- 4,491
- 9
- 32
- 41

- 11
- 1
If variable is an integer between 1 and 65536 (inclusive) then...
if [[ "$port" =~ ^[0-9]+$ && $port -ge 1 && $port -le 65536 ]]; then

- 625
- 8
- 14
^((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)

- 5,061
- 24
- 79
- 125

- 11
- 1