Regex for IPv4 Address
Matches a valid IPv4 address (0-255 blocks).
Understanding The Regex Pattern
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/- 192.168.1.1
- 255.255.255.0
- 256.1.1.1
- 192.168.1
- 192.168.1.1.1
How this Pattern Works
IPv4 address validation is frequently required in network configuration interfaces, server logging systems, access control lists (ACLs), and security firewalls. This complex and highly optimized pattern strictly enforces the structural integrity of a standard IPv4 address, which must consist of exactly four numeric blocks (often referred to as octets) separated by period characters (dots).
To achieve precise validation, this regular expression utilizes non-capturing groups to ensure that each of the four blocks represents a mathematically valid number ranging from 0 up to 255. It cleverly accounts for single digits, double digits, and the specific constraints of the 100s and 200s range. This mathematical constraint is crucial, as a naive regex might allow impossible addresses like 256.0.0.1 or 999.999.999.999, which would cause severe networking failures if processed.
Integrating this regex into your application avoids runtime errors in networking software, ensures that system logs are properly parsed and structured, and maintains strict data quality for IP-based features like geo-location or rate-limiting. It is highly recommended to perform this validation on the client-side to provide immediate feedback to users configuring network settings, while also enforcing it unconditionally on the backend infrastructure to guarantee system stability and security.
Implement The Regex In Your Code
const regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
regex.test('192.168.1.1');import re
regex = re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
bool(regex.match('192.168.1.1'))import "regexp"
matched, _ := regexp.MatchString(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`, "192.168.1.1")use regex::Regex;
let re = Regex::new(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$").unwrap();
re.is_match("192.168.1.1")