Regex in Java: The Ultimate Guide for Developers
In your journey as a Java developer, mastering regular expressions (regex) can significantly elevate your ability to manipulate strings and handle complex text-processing tasks efficiently. Whether it’s validating inputs, searching patterns, or e...

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
1. What is Regex?
1.1. Basic Syntax
- .: Matches any single character except newline.
- *: Matches 0 or more occurrences of the preceding element.
- +: Matches 1 or more occurrences of the preceding element.
- ?: Matches 0 or 1 occurrence of the preceding element.
- []: Matches any one of the characters inside the brackets.
1.2. Example: Matching Email Addresses
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}$";
String email = "test@example.com";
Pattern pattern = Pattern.compile(emailPattern);
Matcher matcher = pattern.matcher(email);
if (matcher.matches()) {
System.out.println("Valid email");
} else {
System.out.println("Invalid email");
}
}
}
1.3. Explanation of the Pattern
- ^: Asserts position at the start of the string.
- [a-zA-Z0-9.%+-]+: Matches any letter, digit, or one of the special characters (.%+-), ensuring at least one character before the "@".
- @: The literal "@" symbol.
- [a-zA-Z0-9.-]+: Matches the domain name, allowing letters, digits, dots, and hyphens.
- .: Escaped dot (.) to match the dot before the domain extension.
- [a-zA-Z]{2,6}: Matches the domain extension (e.g., ".com", ".org"), allowing 2 to 6 characters.
- $: Asserts the position at the end of the string.
Valid email
2. Advanced Regex Techniques
2.1. Capturing Groups
import java.util.regex.*;
public class DateExtractor {
public static void main(String[] args) {
String datePattern = "(\d{2})/(\d{2})/(\d{4})";
String date = "15/09/2024";
Pattern pattern = Pattern.compile(datePattern);
Matcher matcher = pattern.matcher(date);
if (matcher.matches()) {
System.out.println("Day: " + matcher.group(1));
System.out.println("Month: " + matcher.group(2));
System.out.println("Year: " + matcher.group(3));
}
}
}
Day: 15
Month: 09
Year: 2024
2.2. Lookahead and Lookbehind
- Lookahead (?=...): Ensures a match is followed by a specific pattern.
- Lookbehind (?<=...): Ensures a match is preceded by a specific pattern.
String pattern = "\bword\b(?=\d)";
2.3. Optimizing Regex Performance
- Avoid unnecessary backtracking: When using quantifiers like or +, consider using their non-greedy versions (?, +?) if you don’t need to capture the longest possible match.
- Precompile patterns: Instead of creating a new Pattern object for every match, compile it once and reuse it, especially inside loops.
3. Common Pitfalls and How to Avoid Them
3.1. Forgetting to Escape Special Characters
Pattern.compile("."); // Matches a literal dot, not any character
3.2. Using Greedy Quantifiers When Not Necessary
Pattern.compile("."); // Greedy, will match everything
Pattern.compile(".?"); // Non-greedy, will match the smallest possible string
3.3. Not Testing Edge Cases
4. Conclusion
Read more at : Regex in Java: The Ultimate Guide for Developers





