DD
DevDash

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

InputResult
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"
end

php

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

Want API access + no ads? Pro coming soon.