Regex for CIDR Notation (Generic)
Regex Pattern
^[0-9a-fA-F:.]+\/\d{1,3}$Generic CIDR notation for IPv4 or IPv6 with subnet mask
Quick Answer
The regex pattern for cidr notation (generic) is `^[0-9a-fA-F:.]+\/\d{1,3}$`. Generic CIDR notation for IPv4 or IPv6 with subnet mask. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 10.0.0.0/8 | ✓ Matches |
| 192.168.1.0/24 | ✓ Matches |
| 2001:db8::/32 | ✓ Matches |
| 10.0.0.0 | ✗ No match |
| /24 | ✗ No match |
| not-cidr | ✗ No match |
Code Examples
javascript
const regex = /^[0-9a-fA-F:.]+\\/\d{1,3}$/;
const isValid = regex.test(value);python
import re
pattern = r'^[0-9a-fA-F:.]+\/\d{1,3}$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[0-9a-fA-F:.]+\/\d{1,3}$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^[0-9a-fA-F:.]+\\/\d{1,3}$/', $value)) {
echo "valid";
}java
String pattern = "^[0-9a-fA-F:.]+\\/\\d{1,3}$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
IPv4 Address
Validates a well-formed IPv4 address (0-255 in each octet)
IPv6 Address
Basic IPv6 address (8 groups of 4 hex digits)
MAC Address
MAC address with colons or dashes as separators
IPv4 CIDR Notation
IPv4 address with CIDR subnet mask (e.g. 10.0.0.0/24)
Private IP Address
RFC 1918 private IPv4 address ranges (10.x, 172.16-31.x, 192.168.x)
Localhost Detection
Detects localhost URLs (localhost, 127.0.0.1, ::1)