Duplicating elements rather than using a quantifier \d\d is \d{2} - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Description

Duplicating elements rather than using a quantifier \d\d is \d{2}

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{2}-\\d\\d");
    Matcher matcher = pattern.matcher("12-12");
    if (matcher.matches())
      System.out.println("Match.");
    else/*from w  ww  . j a  va2  s  .c o  m*/
      System.out.println("Does not match.");
  }

}

Related Tutorials