13

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.

Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125

13 Answers13

56

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

Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
npinti
  • 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
  • 8
    The 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
  • 2
    Hi, @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
14

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. ...
chtimi59
  • 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
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');}" />
hinekyle
  • 822
  • 8
  • 16
3

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}))?)*
wski
  • 305
  • 1
  • 10
2

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}$)*
SysDragon
  • 9,692
  • 15
  • 60
  • 89
jjluke
  • 21
  • 1
2

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:

https://github.com/findhit/proxywrap/issues/13

saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
lavaGolem
  • 23
  • 6
2
"^((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.

Bugs
  • 4,491
  • 9
  • 32
  • 41
anon
  • 367
  • 1
  • 4
  • 18
2

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 !

Teo
  • 21
  • 2
1

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

Leon Adler
  • 2,993
  • 1
  • 29
  • 42
1

@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])$
Shane Rowatt
  • 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
  • 1
    I 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
1

The solution:

Dim Minlenght As Integer = 1
Dim Maxlenght As Integer = 65536

Regex.IsMatch(sInput,"(^\d{0},{1}$)", "{" + Minlenght, Maxlenght + "}")
Bugs
  • 4,491
  • 9
  • 32
  • 41
1

If variable is an integer between 1 and 65536 (inclusive) then...

if [[ "$port" =~ ^[0-9]+$ && $port -ge 1 && $port -le 65536 ]]; then
NOYB
  • 625
  • 8
  • 14
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][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:====

  1. (6553[0-5]) : 65530-65535
  2. (655[0-2][0-9]) : 65500-65529
  3. (65[0-4][0-9]{2}): 65000-65499
  4. (6[0-4][0-9]{3}) : 60000-64999
  5. ([1-5][0-9]{4}) : 10000-59999
  6. ([0-5]{0,5}) : 00000-55555 (for leading Zeros)
  7. ([0][0-9]{1,4}) : 00000-09999 (for leading Zeros)
  8. ([0-9]{1,4}) : 0000-9999 (for leading Zeros)
Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125