Home > Java > javaTutorial > body text

Example: Use Java regular expressions to match email addresses and mobile phone numbers

PHPz
Release: 2023-12-26 08:28:18
Original
937 people have browsed it

Example: Use Java regular expressions to match email addresses and mobile phone numbers

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}";
Copy after login

where:

  • "\w " matches one or more letters, numbers, or underscores, Represents the part of the user name;
  • "(\w .) " matches one or more letters, numbers, or underscores followed by a dot, representing the part of the domain name;
  • "[a-zA- Z]{2,4}" matches 2 to 4 letters, indicating the domain name suffix.

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 + "不是一个有效的邮箱地址");
        }
    }
}
Copy after login

Run the above code to get the following output:

test123@example.com是一个有效的邮箱地址
Copy after login

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}";
Copy after login

where:

  • "1" means the mobile phone number starts with 1;
  • "[3-9]" means that the second digit can be any number between 3 and 9;
  • "\d{9}" means that the following 9 digits can be any number.

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 + "不是一个有效的手机号码");
        }
    }
}
Copy after login

Run the above code to get the following output:

13812345678是一个有效的手机号码
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template