CIDR to Regex & Range
Paste a CIDR subnet and I generate a regular expression matching exactly its IPs, the range (first–last), the total and examples. Great for grep on logs, ACLs and rules.
Regular expression
Anchored with ^…$ and dots escaped. Use it in
grep -E, egrep or log filters.
Tested examples
| IP | Matches? | Note |
|---|
Checked against the regex itself in your browser.
How to use it · grep, ACLs and filters
Runs locally in your browser · no sign-up · nothing leaves your browser
How it works
The tool takes an IPv4 subnet in CIDR notation (for example 192.168.1.0/24) and generates a regular expression anchored with ^…$ that matches exactly the addresses in the block, no more and no fewer. It also returns the full range (first and last address), the dotted-decimal mask, the total address count (2^(32−prefix)) and the usable hosts.
To build the regex it first computes the network address and broadcast of the block, then breaks each octet down into exact sub-patterns by digit count (units, tens and hundreds), merging complete tens into classes such as 1\d\d. Every example in the table is verified against the generated regex in your browser, so what you see is tested, not assumed.
Example: regex for 192.168.1.0/26
- The /26 block has
2^(32−26) = 64addresses: from 192.168.1.0 to 192.168.1.63, with mask255.255.255.192. - The first three octets are fixed (192, 168 and 1); only the last one varies, from 0 to 63.
- The 0–63 range breaks down into
[1-9]?\d(0–9 plus the merged tens 10–59) and6[0-3], so the final regex is^192\.168\.1\.([1-9]?\d|6[0-3])$, ready forgrep -E.
Frequently asked questions
How do I filter a log by a whole subnet with grep?
grep -E '^10\.0\.0\.…$' access.log. If the IP is not alone on the line (for example in an Apache log), remove the ^ and $ anchors; the tool shows both variants in the usage section.Why does the regex start with ^ and end with $?
^…$ the expression only accepts lines that are exactly one IP of the block.