Use ranges to build a class that accepts only characters that appear in real words - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

\w class allows underscores.

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("[a-zA-Z0-9]");
    Matcher matcher = pattern.matcher("2");
    if (matcher.matches())
      System.out.println("Match.");
    else/* w  w w  . j  ava  2s  .co  m*/
      System.out.println("Does not match.");
  }

}

Related Tutorials