Advertisement

Pattern library / Network & URLs

Regex to Match an IP Address with Port

10.0.0.1:443 is two validated things glued together: an IPv4 address and a port number. The pattern below checks both ranges — octets stop at 255, ports stop at 65535 — and the \b boundaries let it fire in the middle of a log line without catching a bare port 443. Valid RE2, so the same pattern runs in GA4, BigQuery, Go and Rust.

The pattern
\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]):(?: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}|[1-9][0-9]{0,3}|0)\b
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
GET / 200 from 10.0.0.1:443Matchvalid IPv4 plus port inside a log lineTry →
backend listening on 192.168.1.20:8080Matchprivate address with a four-digit portTry →
port 443 is openNo matcha port on its own is not ip:portTry →
blocked client 10.0.0.999:443No matchoctet 999 is above 255Try →
timeout from 10.0.0.1:65536No matchport 65536 is above 65535Try →
retry 10.0.0.1:80 then 10.0.0.1:443Matchthe first pair matches; add the g flag to collect bothTry →

Where to use it

JavaScript

const re = /\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]):(?: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}|[1-9][0-9]{0,3}|0)\b/g;
'retry 10.0.0.1:80 then 10.0.0.1:443'.match(re);  // ['10.0.0.1:80', '10.0.0.1:443']

Python

import re
pat = r'\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]):(?: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}|[1-9][0-9]{0,3}|0)\b'
re.findall(pat, 'backend on 192.168.1.20:8080')  # ['192.168.1.20:8080']

BigQuery / GA4 (RE2)

SELECT REGEXP_CONTAINS('backend on 192.168.1.20:8080',
  r'\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]):(?: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}|[1-9][0-9]{0,3}|0)\b')

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

How do I match an IP address with a port?

Match four range-checked octets, a literal colon, then a range-checked port: \b(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\. … :(?:6553[0-5]|…|0)\b (full pattern in the box above). Paste it with your own log line into the tester to confirm before you ship it.

Why doesn't my pattern match 10.0.0.999:443?

Because 999 is not a valid octet — the address stops being an IPv4 address at 255. The same logic rejects 10.0.0.1:65536: ports run 0-65535. That rejection is the point; a looser pattern matches and then the parsed port fails later.

Does this work in Google Analytics 4?

Yes — groups, alternation and bounded repetition are all RE2 features, so this pattern runs in GA4, BigQuery, Go and Rust. Remember GA4 full-matches by default: drop the word boundaries and anchor it with ^…$ when the dimension value must be exactly one ip:port.

IPv4 Address Regex — Validate Dots & Octets · IPv6 Regex — Full & Compressed Addresses (RE2-Safe) · Log Level Regex — Match INFO / WARN / ERROR Lines

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.