Regex to Match Port Numbers

Validate TCP/UDP port numbers in the range 0-65535 with this regex pattern. Ensures the number falls within the valid port number range. Free.

Regular Expression

/\b(?:[0-9]{1,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])\b/g

Token Breakdown

TokenDescription
\bWord boundary assertion
(?:Start of non-capturing group
[0-9]Character class — matches any one of: 0-9
{1,4}Matches between 1 and 4 times
|Alternation — matches the expression before OR after the pipe
[1-5]Character class — matches any one of: 1-5
[0-9]Character class — matches any one of: 0-9
{4}Matches exactly 4 times
|Alternation — matches the expression before OR after the pipe
6Matches the literal character '6'
[0-4]Character class — matches any one of: 0-4
[0-9]Character class — matches any one of: 0-9
{3}Matches exactly 3 times
|Alternation — matches the expression before OR after the pipe
6Matches the literal character '6'
5Matches the literal character '5'
[0-4]Character class — matches any one of: 0-4
[0-9]Character class — matches any one of: 0-9
{2}Matches exactly 2 times
|Alternation — matches the expression before OR after the pipe
6Matches the literal character '6'
5Matches the literal character '5'
5Matches the literal character '5'
[0-2]Character class — matches any one of: 0-2
[0-9]Character class — matches any one of: 0-9
|Alternation — matches the expression before OR after the pipe
6Matches the literal character '6'
5Matches the literal character '5'
5Matches the literal character '5'
3Matches the literal character '3'
[0-5]Character class — matches any one of: 0-5
)End of group
\bWord boundary assertion

Detailed Explanation

This regex validates port numbers by ensuring the value is within the valid range of 0 to 65535. Here is the token-by-token breakdown:

\b — A word boundary anchor to prevent matching port numbers embedded within larger numbers.

The core of the pattern is a non-capturing group with six alternatives, each handling a different numeric range:

[0-9]{1,4} — Matches numbers with one to four digits, covering 0 to 9999.

[1-5][0-9]{4} — Matches five-digit numbers starting with 1-5, covering 10000 to 59999.

6[0-4][0-9]{3} — Matches numbers from 60000 to 64999. The first digit is 6, the second is 0-4, followed by any three digits.

65[0-4][0-9]{2} — Matches numbers from 65000 to 65499. Narrows the range further with the third digit being 0-4.

655[0-2][0-9] — Matches 65500 to 65529. The fourth digit is restricted to 0-2.

6553[0-5] — Matches 65530 to 65535, the maximum valid port number.

\b — A closing word boundary anchor.

The g flag enables global matching. This pattern is useful for validating network configurations, firewall rules, and connection strings. Well-known ports (0-1023) are reserved for system services, registered ports (1024-49151) are assigned by IANA, and dynamic ports (49152-65535) are used for temporary connections.

Example Test Strings

InputExpected
80Match
65535Match
65536No Match
443Match
99999No Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

80 65535 65536 443 99999

Matches & Capture Groups

#180index 0
#265535index 3
#3443index 15
Pattern: 87 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →