Use java.util.regex.Pattern, java.util.regex.Matcher to check the phone number

Brief introduction

  Use these two methods to verify the phone number format is correct, of course, it can not only verify the phone number, you can also check other data formats have limits.

Pattern: Format

public final class Pattern categories: This is a format class, object-oriented perspective, he is a format class, called an object of his format.

Regular Expressions: Human specified format

Regular expression "1 \\ d {10}", begins with the number 1, followed by 10 digits. In Java \ d represents a number matches, {10}: matching 10 times. \: Indicates that the following character is a special character. So \ (encounter special characters) \ d (special characters)

By compile ( "1 \\ d {10}"), the object to obtain a desired format.

Pattern  pattern = Pattern.compile("1\\d{10}");

Matcher: class matching, matching is completed work

Patter and through a matching object is to obtain a match, a match of the match to complete the work.

Matches matches = pattern.matcher (Mobile);

Returns true if the mobile conform to the format, otherwise false.

boolean t = matcher.matchers ();

Guess you like

Origin www.cnblogs.com/deijiawoyu/p/12634986.html