Using predefined character classes - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

The predefined character classes are shown in the following table.

Regex Matches
. Any character
\d Any digit (0-9)
\D Any non-digit (anything other than 0-9)
\s Any white space character (spaces, tabs, newlines, returns, and backspaces)
\S Any character other than a white space character
\w Any word character (a-z, A-Z, 0-9, or an underscore)
\W Any character other than a word character

Demo Code

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

public class Main {
  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("c.t");
    Matcher matcher = pattern.matcher("cat");
    if (matcher.matches())
      System.out.println("Match.");
    else/*from  www  .j a va 2  s. co  m*/
      System.out.println("Does not match.");
  }

}

Related Tutorials