liveBook · Manning

IPv4

As explained in Regular Expression Puzzles and AI Coding Assistants

An IPv4 address is a 32-bit Internet Protocol version 4 address conventionally written as four decimal components separated by periods.

What makes `192.168.20.1` an IPv4 address while `292.168.10.1` is not? The dots are the easy part. An IPv4 address is a 32-bit unsigned integer conventionally written as a dotted quad: four decimal byte values, each from 0 through 255. Special or reserved ranges still count as IPv4 addresses; format validation is not a test of whether an address is routable or ordinary. section 5.7

A validator must require exactly four decimal components and reject non-digits, extra components, and values above 255. Thus `5.138.0.21.23` has too many components, `192.AA.20.1` contains non-decimal text, and `292.168.10.1` exceeds the permitted byte range. section 5.7

The tempting regex—four groups of one to three digits—looks plausible but only checks shape. It accepts `992.0.100.13`, even though 992 cannot fit in one byte. Limiting the first digit to 1 or 2 improves matters, yet still accepts `271.10.199.3`. A correct range-aware pattern must distinguish `250`–`255`, `200`–`249`, and the remaining allowed values. section 5.8

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

Inspect a fully anchored regex whose alternation enumerates the allowed forms of one IPv4 component and repeats it for the first three dotted components. section 5.8

A range-aware component pattern can be read as a small case analysis:

  • `25[0-5]` permits only 250 through 255. section 5.8
  • `2[0-4]\d` permits 200 through 249. section 5.8
  • `[01]?\d\d?` covers the remaining one-, two-, and three-digit permitted forms; leading zeros are conventionally omitted but not required here. section 5.8

When a match is not validation

Generated code can be confidently wrong. One suggestion used `\d+` for each named dotted component, accepting `56789.1.0.0987654`; another limited components to one through three digits but still did not enforce the 0–255 range. A stronger Python example tests ordinary addresses, both extremes (`0.0.0.0` and `255.255.255.255`), and a non-address. Treat an AI-produced regex as a candidate to test, not as proof that the constraint was understood. section 5.9

Anchors matter when the goal is to validate the entire input rather than find an address-like substring. Beyond the immediate puzzle, named component groups may be useful if later work needs parts of an address for subnet-related handling—but that is a separate requirement from recognizing a valid dotted quad. Pause before choosing a compact-looking pattern: does it enforce every numeric boundary, and does its matching mode fit the job? section 5.8 section 5.9

How Regular Expression Puzzles and AI Coding Assistants covers it

How deep it goes
Builds a complete range-aware regex from false-positive counterexamples, without exploring IPv4 routing or address allocation.
Examples
Tests valid `192.168.20.1` against out-of-range, extra-component, and non-digit inputs, plus AI-generated regexes.
What it assumes
Assumes familiarity with basic regex syntax while explaining quantifiers, alternation, repetition, and anchors through the validator.
What you can do after
Evaluate/understand whether an IPv4 regex rejects malformed quads rather than merely matching dotted digit sequences.
How it connects
Connects IPv4 validation with regular expressions, anchors, named groups, and subnet prefixes.

Also explained in

Compare how 2 books explain IPv4 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Regular Expression Puzzles and AI Coding Assistants. The section links point to the parts of the book it draws on.