Java regular expression syntax example: matching email and mobile phone numbers, specific code examples are required
Regular expression is a powerful text matching tool that can be used Extract and match required information from text. In Java, using regular expressions requires using the related classes and methods provided by the java.util.regex package. This article will introduce how to use regular expressions to match email addresses and mobile phone numbers, and give specific code examples.
1. Matching email addresses
The format of email addresses is usually "username@domain name", in which both username and domain name have certain rules and restrictions. The following is an example of a regular expression to match common email formats:
String emailRegex = "\w+@(\w+\.)+[a-zA-Z]{2,4}";
where:
Use the above regular expression to quickly determine whether a string conforms to the email format. The sample code is as follows:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmailValidator { private static final String EMAIL_REGEX = "\w+@(\w+\.)+[a-zA-Z]{2,4}"; public static boolean isValidEmail(String email) { Pattern pattern = Pattern.compile(EMAIL_REGEX); Matcher matcher = pattern.matcher(email); return matcher.matches(); } public static void main(String[] args) { String email = "test123@example.com"; if (isValidEmail(email)) { System.out.println(email + "是一个有效的邮箱地址"); } else { System.out.println(email + "不是一个有效的邮箱地址"); } } }
Run the above code to get the following output:
test123@example.com是一个有效的邮箱地址
2. Matching mobile phone numbers
Mobile phone numbers generally consist of 11 digits, starting with 1. The following is an example of a regular expression to match the common mobile phone number format:
String phoneRegex = "1[3-9]\d{9}";
where:
Use the above regular expression to quickly determine whether a string conforms to the mobile phone number format. The sample code is as follows:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneValidator { private static final String PHONE_REGEX = "1[3-9]\d{9}"; public static boolean isValidPhone(String phone) { Pattern pattern = Pattern.compile(PHONE_REGEX); Matcher matcher = pattern.matcher(phone); return matcher.matches(); } public static void main(String[] args) { String phone = "13812345678"; if (isValidPhone(phone)) { System.out.println(phone + "是一个有效的手机号码"); } else { System.out.println(phone + "不是一个有效的手机号码"); } } }
Run the above code to get the following output:
13812345678是一个有效的手机号码
Summary:
Through the above example code, we show how to use regular expressions to match email addresses and mobile phone numbers. Regular expressions are a powerful tool that can be used to handle a variety of text matching problems. In practical applications, we can adjust the specific rules of regular expressions as needed to adapt to various data format requirements. I hope this article helps you learn and use regular expressions.
The above is the detailed content of Example: Use Java regular expressions to match email addresses and mobile phone numbers. For more information, please follow other related articles on the PHP Chinese website!