How to Validate an IPv4 Address with Regex?
Validate IPv4 Address is a Regex pattern that this regex pattern validates whether a string is a correctly formatted ipv4 address.. Formula Genius generates and validates this formula automatically from a plain-English prompt.
Validating an IPv4 address can be tricky, but using regex makes it straightforward by ensuring each octet is within the correct range.
The Formula
"Validate an IPv4 address in dotted-decimal notation (e.g. 192.168.1.1) with each octet between 0 and 255"
^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
This regex pattern validates whether a string is a correctly formatted IPv4 address.
Step-by-Step Breakdown
- The pattern starts with ^ to indicate the beginning of the string.
- Each octet is matched using (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?), allowing values from 0 to 255.
- The \.(dot) is used to separate each octet.
- The pattern repeats for four octets, ensuring the full address is validated.
- The pattern ends with $ to indicate the end of the string.
Edge Cases & Warnings
- Input with octets exceeding 255 (e.g., 256.100.50.25)
- Input with non-numeric characters (e.g., 192.abc.1.1)
- Input with fewer or more than four octets (e.g., 192.168.1 or 192.168.1.1.1)
- Input with leading zeros (e.g., 192.168.01.1)
- Input with spaces (e.g., 192. 168.1.1)
Examples
"192.168.1.1"
Valid
"256.100.50.25"
Invalid
Frequently Asked Questions
What does this regex pattern validate?
It validates whether a string is a correctly formatted IPv4 address.
Can this regex handle leading zeros?
No, it does not allow leading zeros in octets.
What happens if the input has non-numeric characters?
The regex will return invalid for any non-numeric characters.
Can't find what you need?
Describe any formula in plain English and Formula Genius will generate, explain, and validate it — instantly.