\w matches word character (a-z, A-Z, 0-9, or an underscore) and \W matches non word character - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

That includes upper- and lowercase letters, digits, and the underscore. For example:

The following pattern matches two groups of word characters separated by a non-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("\\w\\w\\w\\W\\w\\w\\w");
    Matcher matcher = pattern.matcher("abc def");
    if (matcher.matches())
      System.out.println("Match.");
    else/*  w ww  . j av a2  s . co  m*/
      System.out.println("Does not match.");
  }

}

Related Tutorials