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.
\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
\b— a word boundary. It anchors the address to the start of a token, so the pattern does not fire insidevhost10.0.0.1:443(letter straight into digit is no boundary). Leading-or.is fine.- The octet group
(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])reads top-down: 250-255, then 200-249, then 100-199, then 0-99. Greedy left-to-right ordering is what stops999from sliding through as two digits. - The port group does the same job for 0-65535:
6553[0-5]→ 65530-65535,[1-5][0-9]{4}→ 10000-59999,[1-9][0-9]{0,3}→ 1-9999,0→ port 0. - No lookaround and no backreferences, so it is plain RE2. Add the
gflag in JavaScript (orre.findallin Python) to collect every ip:port pair instead of only the first.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
GET / 200 from 10.0.0.1:443 | Match | valid IPv4 plus port inside a log line | Try → |
backend listening on 192.168.1.20:8080 | Match | private address with a four-digit port | Try → |
port 443 is open | No match | a port on its own is not ip:port | Try → |
blocked client 10.0.0.999:443 | No match | octet 999 is above 255 | Try → |
timeout from 10.0.0.1:65536 | No match | port 65536 is above 65535 | Try → |
retry 10.0.0.1:80 then 10.0.0.1:443 | Match | the first pair matches; add the g flag to collect both | Try → |
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
- The shortcuts people reach for are wrong:
[0-9]{1,3}happily accepts octet999and[0-9]{1,5}accepts port65536. If you only need to find something shaped like ip:port in trusted data, fine — but a validation check needs the range groups above. - IPv4 only.
[2001:db8::1]:8443never matches here — the::compression and the bracketed form break the four-octet shape. Use the IPv6 pattern for the address, then\]:[0-9]{1,5}\bfor the bracketed port. - In GA4 the pattern full-matches by default. For a field that must be exactly one ip:port, anchor it:
^(?:…)$. Leave the anchors off for log lines — that is what lets the pattern find the address inside a longer message.
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.
Related patterns
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.