Java Regex (Pattern Matching)

Loading

Java Regex (Regular Expressions) is a powerful tool for pattern matching and text manipulation. It allows you to search, extract, and manipulate text based on specific patterns. Java provides the java.util.regex package, which includes the Pattern and Matcher classes for working with regular expressions.


1. Key Components of Java Regex

  • Pattern: A compiled representation of a regular expression.
  • Matcher: An engine that performs match operations on a character sequence using a pattern.
  • Regex Syntax: A set of special characters and constructs used to define patterns.

2. Common Regex Metacharacters

Here are some commonly used metacharacters in regex:

MetacharacterDescription
.Matches any single character except newline.
\dMatches any digit (equivalent to [0-9]).
\DMatches any non-digit character.
\wMatches any word character (alphanumeric + underscore).
\WMatches any non-word character.
\sMatches any whitespace character (space, tab, newline).
\SMatches any non-whitespace character.
^Matches the beginning of a line.
$Matches the end of a line.
*Matches 0 or more occurrences of the preceding character.
+Matches 1 or more occurrences of the preceding character.
?Matches 0 or 1 occurrence of the preceding character.
{n}Matches exactly n occurrences of the preceding character.
{n,}Matches n or more occurrences of the preceding character.
{n,m}Matches between n and m occurrences of the preceding character.
[]Matches any single character within the brackets.
|Acts as an OR operator (e.g., a|b matches a or b).
()Groups multiple characters or expressions.

3. Using the Pattern and Matcher Classes

The Pattern class compiles a regex pattern, and the Matcher class performs match operations on the input text.

Example: Basic Pattern Matching

import java.util.regex.*;

public class RegexExample {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog.";
        String regex = "fox"; // Pattern to match

        Pattern pattern = Pattern.compile(regex); // Compile the regex
        Matcher matcher = pattern.matcher(text); // Create a matcher for the input text

        if (matcher.find()) {
            System.out.println("Pattern found!");
        } else {
            System.out.println("Pattern not found.");
        }
    }
}

4. Common Regex Operations

a. Finding Matches

Use the find() method to search for occurrences of the pattern in the input text.

import java.util.regex.*;

public class FindMatchesExample {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog.";
        String regex = "\\b\\w{5}\\b"; // Matches 5-letter words

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);

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

b. Extracting Groups

Use parentheses () to define groups in the regex and the group() method to extract them.

import java.util.regex.*;

public class ExtractGroupsExample {
    public static void main(String[] args) {
        String text = "John Doe, age 30; Jane Doe, age 25";
        String regex = "(\\w+ \\w+), age (\\d+)"; // Matches name and age

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            System.out.println("Name: " + matcher.group(1) + ", Age: " + matcher.group(2));
        }
    }
}

c. Replacing Text

Use the replaceAll() or replaceFirst() methods to replace matched text.

import java.util.regex.*;

public class ReplaceTextExample {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog.";
        String regex = "fox"; // Pattern to replace

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);

        String replacedText = matcher.replaceAll("cat"); // Replace all occurrences
        System.out.println("Replaced Text: " + replacedText);
    }
}

d. Splitting Text

Use the split() method to split text based on a regex pattern.

import java.util.regex.*;

public class SplitTextExample {
    public static void main(String[] args) {
        String text = "apple,banana,orange,grape";
        String regex = ","; // Pattern to split on

        Pattern pattern = Pattern.compile(regex);
        String[] fruits = pattern.split(text);

        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

5. Common Regex Patterns

a. Email Validation

String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

b. Phone Number Validation

String regex = "^(\\+\\d{1,3}[- ]?)?\\d{10}$"; // Matches +91-1234567890 or 1234567890

c. Password Validation

String regex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$"; // At least 8 characters, 1 digit, 1 uppercase, 1 lowercase, 1 special character

d. Date Validation (YYYY-MM-DD)

String regex = "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$";

6. Best Practices

  • Use predefined character classes (e.g., \d, \w) for readability.
  • Use non-greedy quantifiers (e.g., *?, +?) when appropriate.
  • Test your regex patterns thoroughly using tools like regex101.com.
  • Use comments in complex regex patterns for better maintainability.

7. Example: Validating an Email Address

import java.util.regex.*;

public class EmailValidationExample {
    public static void main(String[] args) {
        String email = "test@example.com";
        String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(email);

        if (matcher.matches()) {
            System.out.println("Valid email address.");
        } else {
            System.out.println("Invalid email address.");
        }
    }
}

By mastering Java Regex, you can efficiently perform pattern matching, text extraction, and manipulation in your applications!

Leave a Reply

Your email address will not be published. Required fields are marked *