Regexdrill

Patterns for common data Updated November 2026

Regex for IPv4 address

A loose IPv4 regex is ^\d{1,3}(?:\.\d{1,3}){3}$, which also accepts 999.999.999.999. The strict version checks each octet is 0 to 255: ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$. It rejects leading zeros such as 01.

Syntax

/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/

Examples

const ip = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
["192.168.0.1", "256.1.1.1", "10.0.0"].map(s => ip.test(s))

Result: [true, false, false]

"host 10.0.0.1 and 172.16.5.40 up".match(/\b(?:\d{1,3}\.){3}\d{1,3}\b/g)

Result: ["10.0.0.1", "172.16.5.40"]

Loose shape with word boundaries is enough for pulling addresses out of logs.

"192.168.0.1".split(".").every(o => /^\d{1,3}$/.test(o) && Number(o) <= 255)

Result: true

Split and compare numerically; easier to read than the big alternation.

Common mistakes

  • Using \d{1,3} alone passes 300.1.1.1; use the 0 to 255 alternation or check each octet as a number after splitting.
  • Forgetting to escape the dot, so 1a2b3c4 matches; write \. for a literal dot.
  • Putting [1-9]?\d first in the alternation makes the engine try the short option and backtrack; order from most specific (25[0-5]) to least.
  • Using a capturing group with {3} and expecting three captures; a repeated group keeps only its last match.

Practice Regex for IPv4 address

These drills check your answer on live data, free, no account needed.

  • IPv4 addresses, 0 to 255The whole line must match. Match four numbers from 0 to 255 separated by dots. No leading zeros, so 01 is not allowed.
  • Repeated group: dotted numbersThe whole line must match. Match one or more numbers separated by single dots, like 1, 1.2 or 10.0.3. No leading, trailing or doubled dots.
  • Alternation: cat, dog or birdThe whole line must match. Match exactly one of the words cat, dog or bird.

Questions

How do I match an IPv6 address with regex?

The full form is ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$. Compressed forms with :: and embedded IPv4 make the complete pattern very long; use a parsing library or the net module in Node (net.isIP).

Does the strict IPv4 regex accept leading zeros like 192.168.01.1?

No. [1-9]?\d allows a single 0 but not 01. If you need to accept zero-padded octets, use (?:25[0-5]|2[0-4]\d|[01]?\d\d?) for each part.

What is the regex for an IP address with a CIDR suffix?

Append \/(?:3[0-2]|[12]?\d) to the IPv4 pattern before the $ to accept /0 through /32, as in 10.0.0.0/8.

Related

  • Alternation |How the pipe works, why ^cat|dog$ does not do what it looks like, how to group alternatives, and why option order matters when one is a prefix of another.
  • Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.
  • Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
  • Non-capturing group (?:...)How (?:...) groups for quantifiers and alternation without a numbered capture, why that keeps group numbers stable, and how it changes findall in Python.

Previous: Regex for URLNext: Regex for password