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
| Token | Description |
|---|---|
| \b | Word 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 |
| 6 | Matches 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 |
| 6 | Matches the literal character '6' |
| 5 | Matches 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 |
| 6 | Matches the literal character '6' |
| 5 | Matches the literal character '5' |
| 5 | Matches 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 |
| 6 | Matches the literal character '6' |
| 5 | Matches the literal character '5' |
| 5 | Matches the literal character '5' |
| 3 | Matches the literal character '3' |
| [0-5] | Character class — matches any one of: 0-5 |
| ) | End of group |
| \b | Word 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
| Input | Expected |
|---|---|
| 80 | Match |
| 65535 | Match |
| 65536 | No Match |
| 443 | Match |
| 99999 | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
87 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match IPv4 Addresses
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Regex to Match CIDR Notation
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/(?:3[0-2]|[12]?\d)\b/g
Regex to Match Integers
/^-?\d+$/
Regex to Match IPv6 Addresses
/(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/g