Create ranges for pattern with [a-z] - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

Custom character classes can also specify ranges of letters and numbers.

The pattern says that the string can be two characters long.

The first must be a character from a through z, and the second must be from 0 through 5.

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-z][0-5]");
    Matcher matcher = pattern.matcher("r2");
    if (matcher.matches())
      System.out.println("Match.");
    else/*from   w  w w .  java2s . c  o m*/
      System.out.println("Does not match.");
  }
}

Related Tutorials