regex for port number

matches a port number in computer networks

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:

  1. Take the input string and convert it into a number
  2. If possible see if it falls in the range 0-65535
  3. If both the conditions succeed, then it is a valid port number

Cheatsheet

expr usage
iHateRegex
by geon