Python (re)

TCP/UDP Port Number in PY

Match port numbers (1–65535) following a colon, as you'd find in host:port strings.

Try it in the PY tester →

Pattern

regexPY
:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4})\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r":(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4})\b")
input_text = "http://localhost:8080"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

The alternation enforces the 0–65535 range. Requires a leading colon for context, followed by a word boundary.

Examples

Input

http://localhost:8080

Matches

  • :8080

Input

db.example.com:5432

Matches

  • :5432

Input

api:65535

Matches

  • :65535

Same pattern, other engines

← Back to TCP/UDP Port Number overview (all engines)