How do I use predefined character classes regex?

In regex, you also have a number of predefined character classes that provide you with a shorthand notation for commonly used sets of characters.

Here are the list:

  • : represent any character.
  • d : represent any digit, shorthand for [0-9]
  • D : represent a non digit, [^0-9]
  • s : represent a whitespace character [^s]
  • S : represent any non whitespace character
  • w : represent word character [a-zA-Z_0-9]
  • W : represent a non word character


package org.kodejava.example.util.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PredefinedCharacterClassDemo {
    public static void main(String[] args) {
        // Define regex that will search a whitespace followed by f 
        // and two any characters.
        String regex = "\sf..";

        // Compiles the pattern and obtains the matcher object.
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(
                "The quick brown fox jumps over the lazy dog");

        // find every match and print it
        while (matcher.find()) {
            System.out.format("Text "%s" found at %d to %d.%n",
          , matcher.start(), matcher.end());

This program output the following result:

Text " fox" found at 15 to 19.

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🙂.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.