Check a given string regex pattern is valid or not
try {
// Try to compile the regex pattern
Pattern.compile(patternString);
System.out.println("Valid");
} catch (PatternSyntaxException e) {
// If there's a syntax error, catch it and print Invalid
System.out.println("Invalid");
}
Simple Matching
Matching a phone number
Regex: \\d{3}-\\d{3}-\\d{4}
import java.util.regex.*;
public class SimpleMatchingExample {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\d{3}-\\d{3}-\\d{4}");
Matcher matcher = pattern.matcher("My phone number is 123-456-7890.");
if (matcher.find()) {
System.out.println("Phone number found: " + matcher.group()); //Phone number found: 123-456-7890
} else {
System.out.println("No phone number found.");
}
}
}
Lookaheads and Lookbehinds
Lookahead and lookbehind are zero-width assertions that allow you to match patterns based on what comes before or after a string without consuming characters.
Lookahead ((?=...)): Ensures a match is followed by a specific pattern.
Negative Lookahead ((?!...)): Ensures a match is not followed by a specific pattern.
Lookbehind ((?<=...)): Ensures a match is preceded by a specific pattern.
Negative Lookbehind ((?<!...)): Ensures a match is not preceded by a specific pattern.
Lookahead Example
Match a word that is followed by a specific pattern (e.g., match the word "apple" only if it’s followed by "pie").
Pattern pattern = Pattern.compile("apple(?= pie)");
Matcher matcher = pattern.matcher("I like apple pie and apple juice.");
while (matcher.find()) {
System.out.println("Matched: " + matcher.group()); //Matched: apple
}
Negative Lookahead Example
Match the word "apple" only if it is not followed by "pie".
Pattern pattern = Pattern.compile("apple(?! pie)");
Matcher matcher = pattern.matcher("I like apple pie and apple juice.");
while (matcher.find()) {
System.out.println("Matched: " + matcher.group()); //Matched: apple
}
Lookbehind Example
Match a word that is preceded by a specific pattern (e.g., match "pie" only if it is preceded by "apple").
Pattern pattern = Pattern.compile("(?<=apple )pie");
Matcher matcher = pattern.matcher("I like apple pie and apple juice.");
while (matcher.find()) {
System.out.println("Matched: " + matcher.group()); //Matched: pie
}
Negative Lookbehind Example
Match "pie" only if it is not preceded by "apple".
Pattern pattern = Pattern.compile("(?<!apple )pie");
Matcher matcher = pattern.matcher("I like cherry pie and apple pie.");
while (matcher.find()) {
System.out.println("Matched: " + matcher.group()); //Matched: pie
}
Capturing Groups
Capturing groups allow us to extract specific portions of a matched string.
Match a date in the format MM/DD/YYYY and extract the month, day, and year.
Pattern pattern = Pattern.compile("(\\d{2})/(\\d{2})/(\\d{4})");
Matcher matcher = pattern.matcher("The date is 12/25/2024.");
if (matcher.find()) {
System.out.println("Month: " + matcher.group(1)); //12
System.out.println("Day: " + matcher.group(2)); //25
System.out.println("Year: " + matcher.group(3)); //2024
}
Named Capturing Groups to extractmonth, day, and year.
We can name capturing groups for easier access.
Pattern pattern = Pattern.compile("(?<month>\\d{2})/(?<day>\\d{2})/(?<year>\\d{4})");
Matcher matcher = pattern.matcher("The date is 12/25/2024.");
if (matcher.find()) {
System.out.println("Month: " + matcher.group("month")); //12
System.out.println("Day: " + matcher.group("day")); //25
System.out.println("Year: " + matcher.group("year")); //2024
}
Nested Capturing Groups toextract first and last names from full name
Match a full name and extract first and last names.
The Matcher.replaceAll() and Matcher.replaceFirst() methods allow us to replace matched text in the input string.
Replace all instances of the word "apple" with "orange"
Pattern pattern = Pattern.compile("apple");
Matcher matcher = pattern.matcher("I like apple pie and apple juice.");
String result = matcher.replaceAll("orange");
System.out.println(result); // I like orange pie and orange juice.
Using Groups in Replacement to reverse the order of the first and last names
Pattern pattern = Pattern.compile("(\\w+) (\\w+)");
Matcher matcher = pattern.matcher("John Doe");
String result = matcher.replaceAll("$2, $1");
System.out.println(result); //Doe, John
Conditional Replacement with a Function to transform the text
Using replaceAll() with a function, we can conditionally transform the text.
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("I have 12 apples and 5 bananas.");
String result = matcher.replaceAll(match -> {
return Integer.parseInt(match.group()) * 2 + "";
});
System.out.println(result); //I have 24 apples and 10 bananas.
Practical Use Cases
1. Validating Email Addresses
A common use case is validating whether an email address is in a valid format.