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