Java Regex - Matching Character Classes


Following are various examples of matching character classes using regular expression in java.

Sr.No Construct & Matches
1 [abc]

a, b, or c (simple class).

2 [^abc]

Any character except a, b, or c (negation).

3 [a-zA-Z]

a through z or A through Z, inclusive (range).

4 [a-d[m-p]]

a through d, or m through p: [a-dm-p] (union).

5 [a-z&&[def]]

d, e, or f (intersection).

6 [a-z&&[^bc]]

a through z, except for b and c: [ad-z] (subtraction)

7 [a-z&&[^m-p]]

a through z, and not m through p: [a-lq-z](subtraction).