\d class represents a digit and use it to validate a U. S. Social Security number, XXX-XX-XXXX - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

The regex pattern specifies that the string must contain three digits, a hyphen, two digits, another hyphen, and four digits.

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\\d-\\d\\d\\d\\d");
    Matcher matcher = pattern.matcher("779-12-1212");
    if (matcher.matches())
      System.out.println("Match.");
    else//from  w w w . j  av  a2s .c  o  m
      System.out.println("Does not match.");
  }

}

Related Tutorials