regex for port number
matches a port number in computer networks
^((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}))$
Regex Flags
global match
?
ignore case
?
multiline
?
dotAll
?
unicode
?
sticky
?
8080
3000
65535
65536
A port is a communication endpoint associated with a specific address
A port is a 16-bit unsigned integer ranging from 0
to 65535
.
The regex pattern tries to figure out if the input string falls under the given range.
A Better approach
Although this works, a far better(and simpler) approach would be this:
- Take the input string and convert it into a number
- If possible see if it falls in the range
0-65535
- If both the conditions succeed, then it is a valid port number
Cheatsheet
expr | usage |
---|---|
/\w/ | matches any word character (a-z, A-Z, 0-9, _) |
/[0-9]/ | matches all digits |
/(hello){1,3}/ | matches "hello" that occur between 1 and 3 times (inclusive) |
/^/ | matches beginning of a line |
/$/ | matches end of a line |
iHateRegex
by
geon