Logo Wand.Tools

Regular Expression Generator

Intelligently generate and explain regular expressions, supporting various common pattern matching

Java Regular Expressions (Regex) Tutorial

Complete Java Regular Expressions Tutorial (with Examples) | Java Regex Explained

Java Regular Expressions (Regex) are a powerful tool for text processing, widely used for string matching, validation, replacement, and more. This article provides the most practical Java Regex tutorial, accompanied by detailed code examples, to help you master Java Regex skills and easily handle various development scenarios.


✅ Regular Expression Basic Syntax

Expression Description
. Matches any character except newline
^ Matches the beginning of the input string
$ Matches the end of the input string
* Matches the previous element 0 or more times
+ Matches the previous element 1 or more times
? Matches the previous element 0 or 1 time
[] Matches any character within the brackets
[^^] Matches any character not within the brackets
{n} Matches exactly n times
{n,} Matches at least n times
{n,m} Matches between n and m times (inclusive)
\d Matches a digit character, equivalent to [0-9]
\w Matches a word character (letter, number, or underscore), equivalent to [a-zA-Z_0-9]
\s Matches any whitespace character
` `

🚀 Key Classes for Using Regular Expressions in Java

  • java.util.regex.Pattern
  • java.util.regex.Matcher

📌 Basic Example: Checking if a String Matches a Regex

import java.util.regex.*;

public class RegexExample1 {
    public static void main(String[] args) {
        String pattern = "^[a-zA-Z0-9]{6,12}$";
        String input = "abc12345";

        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(input);

        if (m.matches()) {
            System.out.println("Match successful");
        } else {
            System.out.println("Match failed");
        }
    }
}

🧩 Example: Extracting All Email Addresses

import java.util.regex.*;

public class RegexExample2 {
    public static void main(String[] args) {
        String text = "Contact email: [email protected], [email protected]";
        String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}";

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);

        while (m.find()) {
            System.out.println("Found email: " + m.group());
        }
    }
}

🔁 Example: Replacing All Phone Number Formats

import java.util.regex.*;

public class RegexExample3 {
    public static void main(String[] args) {
        String input = "Customer phone: 13812345678, alternate number: 13987654321";
        String regex = "1[3-9]\\d{9}";

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);

        String result = m.replaceAll("***Confidential***");
        System.out.println(result);
    }
}

🔍 Example: Validating Common Formats (Phone, Email, ID Card)

public class RegexValidation {
    public static void main(String[] args) {
        String phone = "13812345678";
        String email = "[email protected]";
        String idCard = "110101199003077777";

        System.out.println(phone.matches("^1[3-9]\\d{9}$"));          // true
        System.out.println(email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")); // true
        System.out.println(idCard.matches("^\\d{17}[\\dXx]$"));       // true
    }
}

🔐 Common Regular Expression Templates (Recommended to Save)

  • Phone number validation: ^1[3-9]\\d{9}$
  • Email validation: ^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$
  • ID Card number validation: ^\\d{17}[\\dXx]$
  • Password strength check: ^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$
  • IP Address validation: ^(\\d{1,3}\\.){3}\\d{1,3}$
  • URL Address validation: ^https?://[\\w.-]+(?:\\.[\\w\\.-]+)+[/#?]?.*$

📈 Common Java Regular Expression Application Scenarios

  • Form validation (phone number, email, password)
  • Text content extraction (log analysis, email address extraction)
  • Sensitive information processing (e.g., replacing ID cards, phone numbers)
  • Batch string replacement and cleaning
  • Data format standardization