RegexCraft

IPv4 address regex

Matches four dot-separated octets, each 0 to 255, without leading zeros. Word boundaries stop it matching inside longer numbers, so 999.1.1.1 is skipped rather than half-matched.

Open in the tester

The pattern

/\b(?:(?: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)\b/g

Sample text, 3 matches

Gateway 10.0.0.1 answered; peer 192.168.1.24 and mirror 172.16.254.3 are healthy, but 999.1.1.1 is not routable.

What it deliberately doesn’t do

  • CIDR suffixes (/24) and ports (:8080) are not included in the match.
  • IPv6 is a different pattern entirely.

Explained, in order

  1. \bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.
  2. (?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.)Non-capturing group — groups the pattern without capturing
  3. (?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)Non-capturing group — groups the pattern without capturing
  4. 25[0-5]|2[0-4]\d|1\d\d|[1-9]?\dAlternation — matches any ONE of the 4 alternatives listed above/below
  5. 25Matches the text "25" literally
  6. [0-5]Character class — matches one character from: '0' through '5'
  7. 2Matches the character '2' literally
  8. [0-4]Character class — matches one character from: '0' through '4'
  9. \dMatches a digit (0–9)
  10. 1Matches the character '1' literally
  11. \dMatches a digit (0–9)
  12. \dMatches a digit (0–9)
  13. [1-9]Character class — matches one character from: '1' through '9'
  14. ?The preceding element matches optionally (zero or one time) (greedy)
  15. \dMatches a digit (0–9)
  16. \.Matches the character '.' literally
  17. {3}The preceding element matches exactly 3 times (greedy)
  18. (?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)Non-capturing group — groups the pattern without capturing
  19. 25[0-5]|2[0-4]\d|1\d\d|[1-9]?\dAlternation — matches any ONE of the 4 alternatives listed above/below
  20. 25Matches the text "25" literally
  21. [0-5]Character class — matches one character from: '0' through '5'
  22. 2Matches the character '2' literally
  23. [0-4]Character class — matches one character from: '0' through '4'
  24. \dMatches a digit (0–9)
  25. 1Matches the character '1' literally
  26. \dMatches a digit (0–9)
  27. \dMatches a digit (0–9)
  28. [1-9]Character class — matches one character from: '1' through '9'
  29. ?The preceding element matches optionally (zero or one time) (greedy)
  30. \dMatches a digit (0–9)
  31. \bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.

In your language

JavaScript

The engine this page was tested with.

const re = /\b(?:(?: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)\b/g;

// Example
const matches = [...str.matchAll(/\b(?:(?: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)\b/g)];
for (const m of matches) {
  console.log(m[0], m.index, m.groups);
}

Python

Translated. Named groups are rewritten to (?P<name>…).

import re

pattern = re.compile(r"\b(?:(?: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)\b")

# Example
for m in pattern.finditer(text):
    print(m.group(0), m.start(), m.groups())

Go

Translated. Go's RE2 has no lookaround or backreferences.

package main

import (
	"fmt"
	"regexp"
)

var re = regexp.MustCompile(`\b(?:(?: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)\b`)

func main() {
	for _, match := range re.FindAllStringSubmatch(text, -1) {
		fmt.Println(match[0], match)
	}
}

Rust

Translated. The regex crate has no lookaround or backreferences.

use regex::Regex;

// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).

fn main() {
    let re = Regex::new(r#"\b(?:(?: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)\b"#).unwrap();

    for m in re.find_iter(text) {
        println!("{} at {}", m.as_str(), m.start());
    }
}