IPv4 Address
Match valid IPv4 addresses with each octet constrained to 0–255.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("(?:(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)", "g");
const input = "192.168.1.1";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)")
input_text = "192.168.1.1"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)`)
input := `192.168.1.1`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?) (flags: g)Raw source: (?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)
How it works
Examples
Input
192.168.1.1Matches
192.168.1.1
Input
255.255.255.0Matches
255.255.255.0
Input
999.999.999.999No match
—Common use cases
- •Network configuration validation
- •Parsing server log files
- •Firewall rule generation
- •IP address extraction from text
Related patterns
IP Address with CIDR Notation
NetworkingMatch IPv4 addresses with CIDR prefix notation such as 10.0.0.0/8 or 192.168.1.0/24.
Private IP Address (RFC 1918)
NetworkingMatch IPv4 addresses in the RFC 1918 private ranges: 10/8, 192.168/16, and 172.16/12.
IPv6 Address
NetworkingMatch full (non-compressed) IPv6 addresses written as eight colon-separated hextets.
MAC Address
NetworkingMatch 48-bit MAC addresses written with colon or hyphen separators (e.g. 00:1A:2B:3C:4D:5E).
Related concepts
Alternation with the | Operator
ConceptThe pipe | matches either the expression on its left or on its right. Combine with groups to alternate over a subpattern.
How to Match a Specific Number of Characters
How-toUse {n} for exactly n, {n,} for n or more, {n,m} for between n and m. Apply to any single token — character, class, or group.
How to Match Multiple Patterns (Alternation)
How-toCombine patterns with | to match either option. Wrap alternatives in a group to scope the alternation correctly.