\D class matches any character that is not a digit. - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

A regex for validating droid names.

The following pattern matches strings that begin with a character that isn't a digit, followed by a character that is a digit, followed by a hyphen, followed by another non-digit character, and ending with a digit.

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("\\D\\d-\\D\\d");
    Matcher matcher = pattern.matcher("R2-D2");
    if (matcher.matches())
      System.out.println("Match.");
    else/* w w  w. j  ava2  s .co  m*/
      System.out.println("Does not match.");
  }
}

Related Tutorials