Advertisement

Pattern library / Validation

IPv4 Address Regex (Octets Limited to 0–255)

The lazy \d{1,3}(\.\d{1,3}){3} happily accepts 999.999.999.999. A usable IPv4 pattern clamps every octet to 0–255 — a longer expression, but the difference between validating and just counting dots.

The pattern
^((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])$
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
192.168.1.1Matchprivate range, validTry →
255.255.255.255Matchbroadcast, max octetsTry →
0.0.0.0Matchall zeros validTry →
256.1.1.1No matchoctet over 255Try →
192.168.1No matchonly 3 groupsTry →
192.168.01.1No matchleading zero rejected (strict)Try →

Where to use it

JavaScript

const IP = /^((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])$/;
IP.test('192.168.1.1'); // true

Python

import re
IP = r'^((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])$'
bool(re.fullmatch(IP, '10.0.0.255'))

Bash (log filtering)

grep -E '\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])\b' access.log

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

Why is my IP regex so long?

Because octets must stop at 255. Any pattern shorter than these four alternations per octet is accepting invalid addresses like 300.1.1.1.

Does this match private IPs like 192.168.x.x?

Yes — it validates structure, not ownership. Range classification (private/public/reserved) is a separate step done in code.

How do I extract IPs from a log line?

Drop the anchors and add word boundaries: \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])\b — copy it straight into the tester.

URL Regex — Match http/https Links (Practical) · Log Level Regex — Match INFO / WARN / ERROR Lines · UUID Regex — Match Any UUID (1-5) Format

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.