This simple package creates a Java Pattern from a list of IP addresses, including support for CIDR block notation and wildcards. Only IPv4 is supported, not IPv6.
The CidrPattern
class exposes compile
and matches
methods similar to Java's built-in Pattern
class:
public static Pattern compile(String cidrs) throws PatternSyntaxException;
public static Pattern compile(String cidrs, int flags) throws PatternSyntaxException;
public static boolean matches(String cidrs, CharSequence input) throws PatternSyntaxException;
The cidrs
input is a comma-separated list of CIDR, wildcard or address range IPv4 expressions of the form:
- IP-Address
/
bits - IP-Address
-
IP-Address - IP-Address
where
- IP-Address is betwen one and four integers 0-255 (or
*
) separated by.
and - bits is a number between 1 and 32 (defaulting to 32 if
/
bits is omitted)
The following are valid CIDR patterns:
10/8,172.16/12,192.168/16
describes the RFC 1918 private address space.10/24
and10.0.0.*
describe the same set of addresses starting with10.0.0.
.10.0.0.0-10.0.0.255
also describes the same set of addresses starting with10.0.0
.142.250.72.196
matches a single IP Address.
import com.cleo.labs.cidr.CidrPattern;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
// ...
try {
Pattern cidr = CidrPattern.compile("192.168.0/24, 10/8");
if (cidr.matcher("192.168.0.33").matches()) {
// success!
}
} catch (PatternSyntaxException e) {
System.err.println(e.getDescription());
}
A helper class RangePattern
may also be used directly if needed. It builds a regex pattern
designed to match a range of integers:
public static Pattern compile(int min, int max);
public static Pattern compile(int min, int max, int flags);
The compiled pattern checks that the input:
- is an integer (optional leading
-
, followed by 1 or more digits), and - has a value >=
min
and <=max
.
Note that min
and max
maybe negative, 0, or positive. If min
is larger than max
, the
returned Pattern matches no inputs. But no PatternSyntaxException
is ever thrown—all
inputs produce a valid Pattern
.
CidrPattern
is packaged as an executable jar
whose main class is an interactive explorer
tool that supports interactive testing of patterns against inputs.
To launch the tool:
java -jar cidr-pattern-1.0.0.jar
The tool supports the following commands:
cidr
pattern — compile pattern as a list of CIDR patternsrange
min max — compile aRangePattern
from min to maxpattern
pattern — compile pattern as ajava.util.regex.Pattern
directlytest
input — test input against the lastcidr
,range
orpattern
entered.
— exit the tool
The cidr-pattern
library is packaged as a single jar
file with no additional dependencies.
It is stored in the GitHub package repository and can be included in your project's pom.xml
as follows:
<dependency>
<groupId>com.cleo.labs</groupId>
<artifactId>cidr-pattern</artifactId>
<version>1.0.0</version>
</dependency>
The cidr-pattern
library is released under the MIT License.