Regex Character Classes in Java

Last Updated : 3 Dec, 2026

Character classes in Java regular expressions define a set of characters that can match a single character in the input. They are commonly used for pattern matching and input validation.

  • Support character sets, ranges, negation, and set operations.
  • Can be combined with quantifiers to match repeated characters.

Example: Checking whether a string contains a lowercase English letter.

Java
public class Geeks{
    public static void main(String[] args){
        String input = "abc123";
        System.out.println(input.matches(".*[a-z].*"));
    }
}

Output
true

Explanation:

  • [a-z] matches a single lowercase English letter.
  • .* matches zero or more characters before and after the letter, excluding line terminators by default.
  • The matches() method returns true because the entire input satisfies the pattern.

Syntax:

[characters]

Here, characters specifies the allowed character set.

Types of Character Classes

Java supports the following character class constructs:

Type

Syntax

Description

Simple

[abc]

Matches a, b, or c.

Range

[a-z]

Matches a lowercase English letter.

Negation

[^abc]

Matches any character except a, b, or c.

Union

[a-zA-Z]

Matches a lowercase or uppercase English letter.

Intersection

[a-z&&[def]]

Matches characters common to both sets (d, e, or f).

Subtraction

[a-z&&[^aeiou]]

Matches lowercase English consonants.

Example: Using different character classes.

Java
public class Geeks{
    public static void main(String[] args){

        // Simple character class
        System.out.println("a".matches("[abc]"));

        // Range
        System.out.println("5".matches("[0-9]"));

        // Negation
        System.out.println("5".matches("[^0-9]"));

        // Union
        System.out.println("A".matches("[a-zA-Z]"));

        // Intersection
        System.out.println("d".matches("[a-z&&[def]]"));

        // Subtraction
        System.out.println("b".matches("[a-z&&[^aeiou]]"));
    }
}

Output
true
true
false
true
true
true

Predefined Character Classes

Java provides predefined character classes for commonly used character sets. The following table describes their default behavior when UNICODE_CHARACTER_CLASS is disabled.

Regex

Description

.

Matches any character except line terminators by default.

\d

Matches a digit [0-9].

\D

Matches a character other than a digit.

\w

Matches a word character [a-zA-Z0-9_].

\W

Matches a non-word character.

\s

Matches ASCII whitespace, including space, tab, newline, vertical tab, form feed, and carriage return.

\S

Matches any character other than ASCII whitespace.

\h

Matches horizontal whitespace.

\H

Matches non-horizontal whitespace.

\v

Matches vertical whitespace.

\V

Matches non-vertical whitespace.

Example: Using predefined character classes.

Java
public class Geeks{
    public static void main(String[] args){
        System.out.println("5".matches("\\d"));
        System.out.println("A".matches("\\D"));
        System.out.println("_".matches("\\w"));
        System.out.println("@".matches("\\W"));
        System.out.println(" ".matches("\\s"));
        System.out.println("A".matches("\\S"));
    }
}

Output
true
true
true
true
true
true

Note: Backslashes must be escaped in Java string literals. For example, the regex \d is written as "\\d" in Java. The definitions of \d, \w, \s, and their negated forms change to support Unicode when UNICODE_CHARACTER_CLASS is enabled.

Quantifiers with Character Classes

Quantifiers specify how many times the preceding character class or other regex element can occur.

Quantifier

Meaning

Example

Description

*

Zero or more

[a-z]*

Matches zero or more lowercase English letters.

+

One or more

[a-z]+

Matches one or more lowercase English letters.

?

Zero or one

[a-z]?

Matches an optional lowercase English letter.

{m}

Exactly m times

[0-9]{3}

Matches exactly three digits.

{m,}

At least m times

[0-9]{2,}

Matches at least two digits.

{m,n}

Between m and n times

[0-9]{2,4}

Matches two to four digits.

Example: Validating strings using character classes and quantifiers.

Java
public class Geeks{
    public static void main(String[] args){

        // Exactly three digits
        System.out.println("123".matches("[0-9]{3}"));

        // One or more lowercase letters
        System.out.println("java".matches("[a-z]+"));

        // Two to four digits
        System.out.println("12345".matches("[0-9]{2,4}"));

        // Letters and digits
        System.out.println("Java123".matches("[a-zA-Z0-9]+"));
    }
}

Output
true
true
false
true

Explanation:

  • [0-9]{3} requires exactly three digits.
  • [a-z]+ matches a sequence of one or more lowercase English letters.
  • [0-9]{2,4} rejects the input because it contains five digits.
  • [a-zA-Z0-9]+ accepts a non-empty sequence of English letters and digits.
Comment