Web
IPv4 Address Regex
An IPv4 address is four decimal octets separated by dots, each between 0 and 255. This pattern checks every octet with explicit ranges, so values such as 256 or 999 are rejected, and it disallows leading zeros like 01 that some tools interpret as octal. It is useful for firewall rule editors, server configuration forms, log parsing and allow-list fields. The documentation ranges 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 are good examples to use in tests. The limit is that it does not understand CIDR suffixes or tell private addresses from public ones.
Pattern (JavaScript)
/^(?:(?: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])$/Starts at the start of the line/string, then ((the text "25", then one of the characters 0-5 OR the character "2", then one of the characters 0-4, then a digit OR the character "1", then a digit, exactly 2 times OR one of the characters 1-9, optionally (zero or one time), then a digit), then the character "."), exactly 3 times, then (the text "25", then one of the characters 0-5 OR the character "2", then one of the characters 0-4, then a digit OR the character "1", then a digit, exactly 2 times OR one of the characters 1-9, optionally (zero or one time), then a digit), then the end of the line/string.
Examples
Matches
- ✓ 192.168.1.1
- ✓ 10.0.0.255
- ✓ 203.0.113.42
Does not match
- ✗ 256.1.1.1
- ✗ 192.168.1
- ✗ 01.2.3.4
Try it
0 matches (JavaScript engine)
192.168.1.1
10.0.0.255
203.0.113.42
256.1.1.1
192.168.1
01.2.3.4Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:(?: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])$/g;
const text = "192.168.1.1";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(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])$')
text = '192.168.1.1'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$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])$/';
$text = '192.168.1.1';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:(?: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])$");
String text = "192.168.1.1";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:(?: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])$`)
text := "192.168.1.1"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:(?: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])$");
var text = "192.168.1.1";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(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])$").unwrap();
let text = "192.168.1.1";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
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])$/
text = '192.168.1.1'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are leading zeros rejected?
- Some parsers treat an octet such as 010 as octal, which turns it into 8 and can bypass security checks. Rejecting leading zeros keeps addresses unambiguous.
- How do I accept a CIDR range like 10.0.0.0/8?
- Append (?:/(?:3[0-2]|[12]?[0-9]))? before the final $ anchor. That allows an optional prefix length from 0 to 32.
- Can I use this to find IP addresses in log files?
- Yes, replace the anchors with \b word boundaries and use a global search. Version numbers like 1.2.3.4 in the same log will also match, so check the context.
Related patterns
- IPv6 Address^(?:(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6}|:(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:))$
- TCP/UDP Port Number^(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})$
- MAC Address^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$
- Data URI^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$