Validating IPv4 and IPv6 addresses with regular expressions
Summary
This article explains how to use regular expressions (regexp) to validate IPv4 and IPv6 addresses. It includes detailed patterns, examples for valid and invalid formats, and combined formats.
Introduction #
When working with networking tools, validating Internet Protocol (IP) addresses is often required. This article provides regular expressions for validating both IPv4 and IPv6 addresses, explains their formats, and provides practical examples.
IPv4 address validation #
An Internet Protocol version 4 (IPv4) address consists of four decimal numbers ranging from 0 to 255, separated by dots. The total length is between 7 and 15 characters. Each segment is called an octet, representing 8 bits, resulting in a 32-bit address.
Valid IPv4 examples #
192.168.1.1
10.0.0.255
172.16.254.3
255.255.255.255
0.0.0.0
Invalid IPv4 examples #
256.100.100.100 # 256 is outside valid range
192.168.1 # Only three octets
192.168.01.1 # Leading zero is not allowed
192.168.1.1.1 # Too many octets
192.168.1. # Trailing dot
Regular expression for IPv4 #
This pattern matches exactly four octets separated by dots, where each octet is a number from 0 to 255. It ensures that numbers like 256 and improperly formatted segments are rejected, but allows flexibility in leading zeros and dot placement.
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$
Test: regexr.com/8ei58
IPv6 address validation #
Internet Protocol version 6 (IPv6) addresses are 128 bits long and written as eight groups of four hexadecimal digits. The groups are separated by colons. A sequence of zeros can be compressed using a double colon ::, but this can only be used once in an address.
Valid IPv6 examples #
2001:0db8:85a3:0000:0000:8a2e:0370:7334
2001:db8::1
::1
fe80::
0:0:0:0:0:0:0:1
Invalid IPv6 examples #
2001::85a3::8a2e # Double '::' not allowed
2001:db8:85a3 # Too few groups
12345::abcd # Invalid hex (12345 is too long)
::1:: # Double compression
fe80:::1 # Triple colon not valid
Regular expression for IPv6 #
This expression matches all valid IPv6 addresses, including those ending with ::, by accounting for every legal combination of omitted and present hextets, with exactly one optional :: compression allowed.
^((?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}| # full address
(?:[0-9A-Fa-f]{1,4}:){1,7}:| # :: suffix
:(?::[0-9A-Fa-f]{1,4}){1,7}| # :: prefix
(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}| # compressed
(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|
(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|
(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|
(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|
[0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,6})|
:(?:(?::[0-9A-Fa-f]{1,4}){1,6}))$
As a single without comments:
^((?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|:(?::[0-9A-Fa-f]{1,4}){1,7}|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,6})|:(?:(?::[0-9A-Fa-f]{1,4}){1,6}))$
Test: regexr.com/8ei5k
Combined IPv4 and IPv6 validation #
Regular expression for both #
This regular expression accurately matches either a valid IPv4 address with decimal octets from 0 to 255 or a valid IPv6 address with proper colon-separated hextets, including support for shorthand :: compression.
^(
(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\. # IPv4 octet 1
(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\. # IPv4 octet 2
(25[0-5]|2[0-4][0-9]|1[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a-f]{1,4}){1,6}|:(:[0-9A-Fa-f]{1,4}){1,6}))$
Test: regexr.com/8ei69
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
What is the difference between IPv4 and IPv6?
IPv4 uses 32-bit addresses represented in dotted decimal format, while IPv6 uses 128-bit hexadecimal addresses separated by colons.
Can I use the same regular expression for IPv4 and IPv6?
A combined expression can be used to match both IP address formats. The article provides a combined regex pattern that matches both address types.
Why do some IPv6 addresses contain double colons ::?
The :: notation is a shorthand to represent a series of consecutive zero blocks in an IPv6 address.
Can an IPv6 address contain letters?
Yes, IPv6 addresses use hexadecimal notation, which includes digits 0-9 and letters A-F.
Are leading zeros allowed in IPv4 addresses?
Technically, leading zeros are discouraged in IPv4 because they can cause misinterpretation. Most validators reject addresses like 192.168.001.001.
Can a valid IP address end with a dot or colon?
No, valid IP addresses cannot end with a dot (.) or colon (:). Such endings invalidate the format.
Further readings #
Sources and recommended, further resources on the topic:
- IETF: RFC 791: Internet Protocol (IPv4)
- IETF: RFC 8200: Internet Protocol, Version 6 (IPv6)
- Wikipedia: IPv4
- Wikipedia: IPv6
License
Validating IPv4 and IPv6 addresses with regular expressions by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/validating-ipv4-and-ipv6-addresses-with-regexp">Validating IPv4 and IPv6 addresses with regular expressions</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.