\s class matches white space characters including spaces, tabs, newlines, returns, and backspaces. - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

This class is useful when you want to allow the user to separate parts of a string in various ways. For example:

The following pattern specifies that the string can be two groups of any three characters separated by one white space 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("...\\s...");
    Matcher matcher = pattern.matcher("abc def");
    if (matcher.matches())
      System.out.println("Match.");
    else/*from   w  ww. j  av a 2  s  .  c  om*/
      System.out.println("Does not match.");
  }

}

Related Tutorials