Regular Expressions
Pattern matching with regex
Interview Relevant: Regex validation and parsing questions
8 min read
Regular Expressions in Java
Regular expressions (regex) provide powerful pattern matching for text processing. Java's regex support is in java.util.regex package.
Key Classes
- Pattern: Compiled regex pattern
- Matcher: Engine for matching operations
- String methods: matches(), replaceAll(), split()
| Pattern | Meaning |
|---|---|
| . | Any character |
| \\d | Digit [0-9] |
| \\w | Word char [a-zA-Z0-9_] |
| \\s | Whitespace |
| * | 0 or more |
| + | 1 or more |
| ? | 0 or 1 |
| {n,m} | n to m times |
ā ļø Escape in Java: Use double backslash! \\d in Java code represents \d in regex.
Code Examples
Basic pattern matching with String.matches()
java
1import java.util.regex.*;
2
3// Simple pattern matching with String.matches()
4String email = "user@example.com";
5boolean valid = email.matches("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
6System.out.println("Valid email: " + valid); // true
7
8// Phone number validation
9String phone = "123-456-7890";
10boolean validPhone = phone.matches("\\d{3}-\\d{3}-\\d{4}");
11System.out.println("Valid phone: " + validPhone); // true
12
13// Check if string contains only letters
14String name = "John";
15boolean onlyLetters = name.matches("[a-zA-Z]+");
16System.out.println("Only letters: " + onlyLetters); // true
17
18// Check if string contains digits
19String text = "Hello123";
20boolean hasDigits = text.matches(".*\\d.*");
21System.out.println("Has digits: " + hasDigits); // trueUsing Pattern and Matcher classes
java
1// Pattern and Matcher for complex operations
2String text = "Email me at john@example.com or jane@test.org";
3String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}";
4
5Pattern pattern = Pattern.compile(regex);
6Matcher matcher = pattern.matcher(text);
7
8// Find all matches
9while (matcher.find()) {
10 System.out.println("Found: " + matcher.group());
11 System.out.println("At position: " + matcher.start() + "-" + matcher.end());
12}
13// Found: john@example.com
14// Found: jane@test.org
15
16// Check if entire string matches
17String email = "test@example.com";
18boolean matches = Pattern.matches(regex, email);
19System.out.println("Full match: " + matches); // trueCapturing groups for extracting data
java
1// Groups and capturing
2String date = "2024-12-25";
3Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
4Matcher matcher = pattern.matcher(date);
5
6if (matcher.matches()) {
7 System.out.println("Year: " + matcher.group(1)); // 2024
8 System.out.println("Month: " + matcher.group(2)); // 12
9 System.out.println("Day: " + matcher.group(3)); // 25
10 System.out.println("Full: " + matcher.group(0)); // 2024-12-25
11}
12
13// Named groups (Java 7+)
14String regex = "(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})";
15Pattern p = Pattern.compile(regex);
16Matcher m = p.matcher("2024-12-25");
17
18if (m.matches()) {
19 System.out.println("Year: " + m.group("year"));
20 System.out.println("Month: " + m.group("month"));
21 System.out.println("Day: " + m.group("day"));
22}String replacement and splitting with regex
java
1// Replacement operations
2String text = "Hello World! Hello Java!";
3
4// Replace all occurrences
5String result1 = text.replaceAll("Hello", "Hi");
6System.out.println(result1); // "Hi World! Hi Java!"
7
8// Replace first occurrence
9String result2 = text.replaceFirst("Hello", "Hi");
10System.out.println(result2); // "Hi World! Hello Java!"
11
12// Replace with groups (back-reference)
13String numbers = "123-456-7890";
14String formatted = numbers.replaceAll("(\\d{3})-(\\d{3})-(\\d{4})", "($1) $2-$3");
15System.out.println(formatted); // "(123) 456-7890"
16
17// Split with regex
18String csv = "apple, banana, cherry, date";
19String[] fruits = csv.split(",\\s*"); // Split on comma + optional spaces
20// ["apple", "banana", "cherry", "date"]
21
22// Common patterns
23String ALPHANUMERIC = "^[a-zA-Z0-9]+$";
24String IP_ADDRESS = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
25String URL = "^(https?://)?[\\w.-]+\\.[a-zA-Z]{2,}(/\\S*)?$";Use Cases
- Input validation (email, phone, passwords)
- Text parsing and extraction
- Log file analysis
- Data cleaning and transformation
- Search and replace operations
- Configuration file parsing
Common Mistakes to Avoid
- Forgetting double backslash in Java strings
- Not escaping special characters (. + * ?)
- Greedy vs lazy matching confusion
- Not compiling frequently used patterns
- Regex too complex (performance issue)
- Not handling PatternSyntaxException