Regular Expressions in Java | Generated by AI

Home PDF

How to Use java.util.regex in Java

The java.util.regex package in Java provides tools for working with regular expressions (regex), which are patterns used to match and manipulate text. This guide will walk you through the basics of using this package, focusing on the two main classes: Pattern and Matcher. I’ll also cover common use cases with examples to help you get started.

Overview of java.util.regex

The java.util.regex package includes:

Additionally, Java’s String class offers regex-based methods for simpler tasks.

Basic Steps to Use java.util.regex

To use regular expressions in Java, follow these steps:

  1. Compile a Pattern: Convert your regex string into a Pattern object.
  2. Create a Matcher: Use the pattern to create a Matcher for your input text.
  3. Perform Operations: Use the matcher to check for matches, find patterns, or manipulate text.

Here’s how it works in practice.

Example 1: Validating an Email Address

Let’s create a simple email validator using a basic regex pattern: ".+@.+\\..+". This pattern matches strings with at least one character before and after an @ symbol, followed by a dot and more characters (e.g., example@test.com).

import java.util.regex.*;

public class EmailValidator {
    public static boolean isValidEmail(String email) {
        // Define the regex pattern
        String regex = ".+@.+\\..+";
        // Compile the pattern
        Pattern pattern = Pattern.compile(regex);
        // Create a matcher for the input string
        Matcher matcher = pattern.matcher(email);
        // Check if the entire string matches the pattern
        return matcher.matches();
    }

    public static void main(String[] args) {
        String email = "example@test.com";
        if (isValidEmail(email)) {
            System.out.println("Valid email");
        } else {
            System.out.println("Invalid email");
        }
    }
}

Explanation

Output: Valid email

Note: This is a simplified email pattern. Real email validation requires a more complex regex (e.g., per RFC 5322), but this serves as a starting point.

Example 2: Finding All Hashtags in a String

Suppose you want to extract all hashtags (e.g., #java) from a tweet. Use the regex "#\\w+", where # matches the literal hashtag symbol and \\w+ matches one or more word characters (letters, digits, or underscores).

import java.util.regex.*;

public class HashtagExtractor {
    public static void main(String[] args) {
        String tweet = "This is a #sample tweet with #multiple hashtags like #java";
        String regex = "#\\w+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(tweet);

        // Find all matches
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Explanation

Output:

#sample
#multiple
#java

Example 3: Replacing Text with Regex

To replace all occurrences of a word (e.g., censoring “badword” with asterisks), you can use the String.replaceAll() method, which uses regex internally.

public class TextCensor {
    public static void main(String[] args) {
        String text = "This is a badword example with badword repeated.";
        String censored = text.replaceAll("badword", "*******");
        System.out.println(censored);
    }
}

Output: This is a ******* example with ******* repeated.

For more complex replacements, use Matcher:

import java.util.regex.*;

public class ComplexReplacement {
    public static void main(String[] args) {
        String text = "Contact: 123-456-7890";
        String regex = "\\d{3}-\\d{3}-\\d{4}"; // Matches phone numbers
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        String result = matcher.replaceAll("XXX-XXX-XXXX");
        System.out.println(result);
    }
}

Output: Contact: XXX-XXX-XXXX

Example 4: Using Groups to Parse Structured Data

Regex groups, defined with parentheses (), allow you to capture parts of a match. For example, to parse a Social Security Number (SSN) like 123-45-6789:

import java.util.regex.*;

public class SSNParser {
    public static void main(String[] args) {
        String ssn = "123-45-6789";
        String regex = "(\\d{3})-(\\d{2})-(\\d{4})"; // Groups for area, group, serial
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(ssn);

        if (matcher.matches()) {
            System.out.println("Area number: " + matcher.group(1));
            System.out.println("Group number: " + matcher.group(2));
            System.out.println("Serial number: " + matcher.group(3));
        }
    }
}

Explanation

Output:

Area number: 123
Group number: 45
Serial number: 6789

You can also use named groups for clarity:

String regex = "(?<area>\\d{3})-(?<group>\\d{2})-(?<serial>\\d{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("123-45-6789");
if (matcher.matches()) {
    System.out.println("Area: " + matcher.group("area"));
    System.out.println("Group: " + matcher.group("group"));
    System.out.println("Serial: " + matcher.group("serial"));
}

Additional Features and Tips

Flags

Modify pattern behavior with flags in Pattern.compile():

String Methods

For simple tasks, use String methods:

Escaping Special Characters

Regex uses special characters like ., *, and ?. To match them literally, escape with \\:

Performance

Compiling a pattern is costly. Reuse Pattern objects when possible:

Pattern pattern = Pattern.compile("\\d+"); // Compile once
for (String input : new String[]{"123", "456"}) {
    Matcher matcher = pattern.matcher(input);
    if (matcher.matches()) {
        System.out.println("Number: " + input);
    }
}

Error Handling

Invalid regex patterns throw PatternSyntaxException. Handle it in production code:

try {
    Pattern pattern = Pattern.compile("["); // Invalid regex
} catch (PatternSyntaxException e) {
    System.err.println("Invalid regex: " + e.getMessage());
}

Summary

This covers the essentials of java.util.regex. For advanced topics like lookaheads or character classes, consult the Java documentation or experiment with specific use cases!


Back 2025.03.06 Donate