Java examples for Regular Expressions:Pattern
The regex pattern specifies that the string must contain three digits, a hyphen, two digits, another hyphen, and four digits.
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."); } }