Regex and cron

Regex for IPv4 Addresses

Validate dotted-quad IPv4 addresses with every octet range-checked inside the pattern.

  • Free foreverNo trial, no paywall, no usage limits.
  • No sign-upOpen the page and start. We never ask for an email to use a tool.
  • PrivateEverything runs in your browser. Your input is never uploaded or stored on our servers.
  • Works offlineOnce the page has loaded, the tool keeps working without a connection.

Pattern

/^(?:(?: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)$/

Regular expression

JavaScript syntax, run by your browser's own engine in a background worker. Press / anywhere to jump here.
Reset to the original pattern and examplesShow cheat sheet: click any token to insert itCopy pattern
Find every match instead of stopping at the first. Needed for matchAll() and for replacing all occurrences.Case-insensitive: /abc/i also matches "ABC" and "aBc".^ and $ match at the start and end of every line, not just the whole string. Use it to test one value per line.Lets . match line breaks too. Without it, . stops at \n and \r.Treats the pattern as Unicode code points: enables \p{…} property escapes and \u{…}, makes emoji count as one character, and rejects sloppy escapes.Only matches starting exactly at lastIndex (position 0 here), never further along. Useful for tokenizers that consume input piece by piece.Records the start and end index of every capture group (match.indices). The tester always shows group positions, so this only changes your code.The upgraded unicode mode (ES2024): set operations like [\p{L}--[a-z]] and [A&&B], string properties like \p{RGI_Emoji}. Cannot be combined with u.
5 matches5 matches

Test string

Matches are highlighted as you type, in alternating colors. Click inside a match (or hover its row below) to color its capture groups.
5 matchesClear test string

Explanation

Every part of the pattern in plain English. Hover a row to highlight that part of the pattern.
Copy explanation

Loading the explainer…

Every match with its position and capture groups.Preview a replacement with $1, $<name> and $& references, then copy the result.Copy a ready-to-run snippet for JavaScript, Python, Go, C#, PHP or Dart.

Matches

Named groups are shown by name. undefined means the group did not take part in that match.
Copy matches as JSONDownload matches.csv
#MatchPosition
1192.168.1.10–11
28.8.8.812–19
310.0.0.25520–30
4255.255.255.25531–46
50.0.0.047–54

Need a secure web app built around this? Talk to our team

Quick answer

The regex for an IPv4 address is ^(?:(?: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)$. Each octet alternative covers part of the 0–255 range (250–255, 200–249, 100–199 and 0–99), so 256 and leading zeros like 01 are rejected. It matches 192.168.1.1 and 255.255.255.255. Use your language's IP parser when you also need to know whether the address is private or reserved.

  • Tested examples
  • Token-by-token breakdown
  • Code in 5 languages
  • Live tester

How to use the IPv4 address regex

  1. 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
  2. 2Check the sample lines in Test string: every line the IPv4 address regex accepts is highlighted, rejected lines stay plain.
  3. 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
  4. 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
  5. 5Validate again on the server: client-side checks give users quick feedback, not security.

How does the IPv4 address regex work?

Read it left to right. Each piece below matches one part of the input:

  • 25[0-5] 250 to 255.
  • |2[0-4]\d or 200 to 249.
  • |1\d\d or 100 to 199.
  • |[1-9]?\d or 0 to 99, with no leading zero (so 01 and 007 fail).
  • (?:…\.){3} Three octets, each followed by a dot.
  • (?:…)$ The fourth octet, then the end of the string.

Which strings does the IPv4 address regex match?

The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.

InputMatches?Why
192.168.1.1YesA private home-network address.
8.8.8.8YesSingle-digit octets.
10.0.0.255YesZero and 255 are valid octets.
255.255.255.255YesThe highest address.
0.0.0.0YesThe lowest (it means "any address" in server configs).
256.1.1.1No256 is out of range.
192.168.1NoOnly three octets.
192.168.01.1NoLeading zeros are rejected: some parsers read them as octal.
1.2.3.4.5NoFive octets.
192.168.1.-1NoNo negative numbers.
192.168.1.1NoLeading space: trim input first.

Why are leading zeros rejected?

Because different software reads them differently. In the classic BSD inet_aton behavior (still followed by some libraries and by ping on many systems), 010.0.0.1 is octal and means 8.0.0.1. Other parsers treat it as decimal 10. That ambiguity has caused real security bugs in allow-lists and SSRF filters, which is why modern parsers such as Python's ipaddress (since 3.9.5) reject leading zeros outright.

This pattern follows the strict rule: each octet is 0 or a number without a leading zero. If you must accept legacy input, normalize it explicitly instead of loosening the regex.

How do I check for private or reserved addresses?

A regex can do it, but a parser is clearer and tracks the official registry. In Python, ipaddress.ip_address(s).is_private; in Go, netip.ParseAddr(s) and addr.IsPrivate(); in Node.js, net.isIPv4(s) for the format plus a CIDR check.

RangeMeaning
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16Private networks (RFC 1918)
127.0.0.0/8Loopback
169.254.0.0/16Link-local (and cloud metadata at 169.254.169.254)
0.0.0.0/8"This network"

Blocking these ranges matters when your server fetches user-supplied URLs.

How do I use the IPv4 address regex in JavaScript, Python, Go, C# and PHP?

The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.

JavaScript and TypeScript

const regex = /^(?:(?: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)$/;

function isValid(value) {
  return regex.test(value);
}

console.log(isValid("192.168.1.1"));

Python

import re

pattern = re.compile(r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\Z")

def is_valid(value: str) -> bool:
    return pattern.search(value) is not None

print(is_valid("192.168.1.1"))
  • \d and \w are written as [0-9] and [A-Za-z0-9_] because in Python they also match digits and letters from other scripts.
  • JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.

Go

package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^(?:(?: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)$`)
	fmt.Println(re.MatchString("192.168.1.1"))
}

C#

using System;
using System.Text.RegularExpressions;

// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = new Regex(@"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\z", RegexOptions.None, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("192.168.1.1"));
  • \d and \w are written as [0-9] and [A-Za-z0-9_] because in .NET they also match digits and letters from other scripts.
  • JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.

PHP

<?php

$pattern = '/^(?:(?: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)$/D';

function isValid(string $value, string $pattern): bool
{
    return preg_match($pattern, $value) === 1;
}

var_dump(isValid('192.168.1.1', $pattern));
  • The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).

Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.

Frequently asked questions

^\d{1,3}(\.\d{1,3}){3}$ checks the dotted shape but accepts 999.999.999.999. The pattern on this page is longer because it limits every octet to 0 to 255.
Append (?:\/(?:3[0-2]|[12]?\d))? before the final $ to allow an optional prefix length from /0 to /32, as in 10.0.0.0/8.
Replace ^ and $ with word boundaries (\b) and add the g flag. Each match is then a valid address wherever it appears in the line.

Last updated by the Playxoft engineering team.

From the engineers who built this tool

Building something bigger than a quick fix?

Auth, payments, dashboards and security are where web apps get hard. We have shipped them many times. Share your idea and we will map out a realistic plan and estimate, free and with no obligation.

More free tools

See all tools

Regex Tester

Test regex live with highlighted matches, plain-English explanations and code export.

Open tool

Cron Expression Generator

New

Build and explain cron schedules, see the next runs in any time zone, copy platform snippets.

Open tool

SQL Formatter

Beautify or minify SQL in 12 dialects, with keyword case, indent and line:column errors.

Open tool

cURL to Python Converter

New

Convert cURL commands into Python requests or httpx code with params, json, files and auth.

Open tool

cURL to Fetch Converter

New

Turn any cURL command into JavaScript fetch() code with headers, JSON, forms and auth.

Open tool

cURL to Axios Converter

New

Convert cURL commands into an Axios request config with params, data, auth and timeouts.

Open tool