1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: Regular Expressions (Regex) are patterns for matching, searching, and manipulating text. Essential for validation (emails, phones), parsing, and data extraction.
When Gmail validates email format: email.matches("^[\\w.-]+@[\\w.-]+\\.[a-z]{2,}$"). Pattern matching in action!
2. Quick Reference
| Pattern | Meaning | Example |
|---|---|---|
. | Any character | a.c matches "abc", "aXc" |
* | Zero or more | ab* matches "a", "ab", "abbb" |
+ | One or more | ab+ matches "ab", "abbb" |
? | Zero or one | ab? matches "a", "ab" |
\\d | Digit [0-9] | \\d+ matches "123" |
\\w | Word char [a-zA-Z0-9_] | \\w+ matches "hello_123" |
\\s | Whitespace | \\s+ matches spaces, tabs |
[] | Character class | [aeiou] matches vowels |
^ | Start of string | ^Hello matches "Hello..." |
$ | End of string | world$ matches "...world" |
3. Interactive & Applied Code
import java.util.regex.*;
public class RegexBasics {
public static void main(String[] args) {
// === BASIC MATCHING ===
String text = "Hello World";
System.out.println(text.matches("Hello.*")); // true (. = any, * = many)
System.out.println(text.matches("hello.*")); // false (case-sensitive)
System.out.println(text.matches("(?i)hello.*")); // true (case-insensitive)
// === String.matches() ===
String phone = "123-456-7890";
String phonePattern = "\\d{3}-\\d{3}-\\d{4}";
System.out.println(phone.matches(phonePattern)); // true
// === String.replaceAll() ===
String messy = "a1b2c3d4";
String lettersOnly = messy.replaceAll("\\d", ""); // "abcd"
String digitsOnly = messy.replaceAll("[a-z]", ""); // "1234"
// === String.split() with regex ===
String csv = "apple, banana, cherry";
String[] fruits = csv.split(",\\s*"); // Split by comma + any spaces
// ["apple", "banana", "cherry"]
// === Pattern and Matcher ===
String input = "The price is $25 and $50";
Pattern pattern = Pattern.compile("\\$\\d+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("Found: " + matcher.group()); // $25, $50
}
// === COMMON VALIDATIONS ===
// Email validation
String emailPattern = "^[\\w.-]+@[\\w.-]+\\.[a-z]{2,}$";
System.out.println("test@example.com".matches(emailPattern)); // true
System.out.println("invalid.email".matches(emailPattern)); // false
// Phone number (US format)
String usPhone = "^\\(\\d{3}\\) \\d{3}-\\d{4}$";
System.out.println("(123) 456-7890".matches(usPhone)); // true
// Password (min 8 chars, 1 digit, 1 letter)
String password = "^(?=.*\\d)(?=.*[a-zA-Z]).{8,}$";
System.out.println("abc12345".matches(password)); // true
System.out.println("abcdefgh".matches(password)); // false (no digit)
// === GROUPS (extracting parts) ===
String dateStr = "2024-12-25";
Pattern datePattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher dateMatcher = datePattern.matcher(dateStr);
if (dateMatcher.matches()) {
System.out.println("Year: " + dateMatcher.group(1)); // 2024
System.out.println("Month: " + dateMatcher.group(2)); // 12
System.out.println("Day: " + dateMatcher.group(3)); // 25
}
}
}⚠️ Common Mistakes
Mistake #1: Forgetting to escape backslashes
"\\d+" // ✅ Correct (Java needs double backslash)
"\d+" // ❌ Invalid escape sequenceMistake #2: matches() checks entire string
"abc123def".matches("\\d+"); // false (entire string must be digits)
"abc123def".matches(".*\\d+.*"); // true
// Use Matcher.find() for partial matching4. The "Interview Corner"
🏆 Q1: "Validate email with regex?"
String pattern = "^[\\w.-]+@[\\w.-]+\\.[a-z]{2,}$";
email.matches(pattern);🏆 Q2: "matches() vs find()?"
matches(): Entire string must match patternfind(): Finds pattern anywhere in string
🏆 Q3: "What's a capturing group?"
Answer: Parentheses () capture matched portions. Access via matcher.group(1), group(2), etc.
🎓 Key Takeaways
✅ \\d digit, \\w word char, \\s whitespace
✅ Use double backslash in Java: \\d+
✅ matches() = full match, find() = partial
✅ Groups () for extracting parts
✅ Pre-compile with Pattern.compile() for reuse