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.
The pattern
//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
\bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.)Non-capturing group — groups the pattern without capturing(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)Non-capturing group — groups the pattern without capturing25[0-5]|2[0-4]\d|1\d\d|[1-9]?\dAlternation — matches any ONE of the 4 alternatives listed above/below25Matches the text "25" literally[0-5]Character class — matches one character from: '0' through '5'2Matches the character '2' literally[0-4]Character class — matches one character from: '0' through '4'\dMatches a digit (0–9)1Matches the character '1' literally\dMatches a digit (0–9)\dMatches a digit (0–9)[1-9]Character class — matches one character from: '1' through '9'?The preceding element matches optionally (zero or one time) (greedy)\dMatches a digit (0–9)\.Matches the character '.' literally{3}The preceding element matches exactly 3 times (greedy)(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)Non-capturing group — groups the pattern without capturing25[0-5]|2[0-4]\d|1\d\d|[1-9]?\dAlternation — matches any ONE of the 4 alternatives listed above/below25Matches the text "25" literally[0-5]Character class — matches one character from: '0' through '5'2Matches the character '2' literally[0-4]Character class — matches one character from: '0' through '4'\dMatches a digit (0–9)1Matches the character '1' literally\dMatches a digit (0–9)\dMatches a digit (0–9)[1-9]Character class — matches one character from: '1' through '9'?The preceding element matches optionally (zero or one time) (greedy)\dMatches a digit (0–9)\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());
}
}