PatternRegex

Match IP:port

Pull an IPv4 address plus its port out of logs, configs and connection strings — the host:port shape nginx, Redis and half of devops config files use.

Last updated

Patterng
\b(?:\d{1,3}\.){3}\d{1,3}:\d{1,5}\b
Open in Regex Tester & Builder

Matches

  • Server listening on 192.168.1.10:8080 for requests
  • connect to 10.0.0.1:443 now
  • edge case 999.999.999.999:99999 unvalidated

Doesn’t match

  • The IP address 192.168.1.10 alone
  • example.com:8080 uses a hostname, not an IP

Why it’s written this way

(?:\d{1,3}\.){3}\d{1,3} repeats a one-to-three-digit octet and a literal dot three times, then a final octet — the same shape as the email and URL patterns elsewhere in this library, just without a domain on the end. The colon and \d{1,5} after it capture the port, allowing anything from a single digit up to five.

Word boundaries on both ends keep the match from starting mid-number inside a longer digit run, so it lines up cleanly against punctuation and whitespace in log lines and config files.

Edge cases to know

  • Octets are not range-checked, so 999.999.999.999 matches even though no such address exists — this is a shape matcher, not a validator.
  • The port is only checked for digit count, not range, so :99999 passes even though valid ports stop at 65535.
  • It has no idea about IPv6 — bracketed addresses like [::1]:8080 need a different pattern entirely.

Related in Patterns