🔍 Regular Expressions in Java: Your Text Detective Toolkit
Imagine you’re a detective with a magical magnifying glass that can find ANY pattern in a book—names, phone numbers, secret codes—in seconds. That’s what Regular Expressions (Regex) do for your programs!
🎯 What Are Regular Expressions?
Think of regex like a super-powered search tool. When you search for “cat” in a document, you find the word “cat.” But what if you want to find ALL phone numbers? Or ALL email addresses? Regular search can’t do that—but regex can!
Simple Example:
- Normal search: Find “cat” → finds only “cat”
- Regex search: Find “any 3 letters” → finds “cat”, “dog”, “bat”, “sun”…
đź§© Pattern and Matcher: The Dynamic Duo
In Java, regex works with two best friends: Pattern and Matcher.
The Story
Imagine Pattern is a cookie cutter 🍪. You design it once with a specific shape. Matcher is the baker who uses that cookie cutter on dough (your text) to find all matching shapes.
graph TD A["Your Regex String"] --> B["Pattern.compile"] B --> C["Pattern Object"] C --> D["pattern.matcher"] D --> E["Matcher Object"] E --> F["Find Matches!"]
How to Use Pattern and Matcher
// Step 1: Create your cookie cutter
Pattern pattern = Pattern.compile("cat");
// Step 2: Apply it to your text
String text = "The cat sat on a mat";
Matcher matcher = pattern.matcher(text);
// Step 3: Find all matches!
while (matcher.find()) {
System.out.println("Found: " +
matcher.group());
}
// Output: Found: cat
Key Matcher Methods
| Method | What It Does |
|---|---|
find() |
Looks for next match |
group() |
Returns what was found |
start() |
Position where match begins |
end() |
Position where match ends |
matches() |
Does entire text match? |
Pro Tip: Use Pattern.CASE_INSENSITIVE to find “Cat”, “CAT”, and “cat” all at once!
Pattern p = Pattern.compile(
"cat",
Pattern.CASE_INSENSITIVE
);
✨ Regex Syntax: The Magic Symbols
Regex has special characters that give it superpowers. Think of them as magic spells!
The Basic Spells
| Symbol | Magic Power | Example |
|---|---|---|
. |
Any single character | c.t → cat, cot, cut |
* |
Zero or more of previous | ca*t → ct, cat, caat |
+ |
One or more of previous | ca+t → cat, caat (not ct) |
? |
Zero or one of previous | colou?r → color, colour |
^ |
Start of text | ^Hello → text must start with Hello |
$ |
End of text | bye$ → text must end with bye |
Character Classes: Groups of Characters
Think of these as teams of characters:
// [abc] means "a OR b OR c"
Pattern p1 = Pattern.compile("[aeiou]");
// Matches any vowel
// [0-9] means "any digit 0 to 9"
Pattern p2 = Pattern.compile("[0-9]");
// Matches: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
// [a-zA-Z] means "any letter"
Pattern p3 = Pattern.compile("[a-zA-Z]");
Shortcut Classes (The Quick Spells)
| Shortcut | Same As | Meaning |
|---|---|---|
\d |
[0-9] |
Any digit |
\D |
[^0-9] |
NOT a digit |
\w |
[a-zA-Z0-9_] |
Word character |
\W |
[^a-zA-Z0-9_] |
NOT word character |
\s |
[ \t\n\r] |
Whitespace |
\S |
[^ \t\n\r] |
NOT whitespace |
Example: Find all digits in text
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("I have 42 apples");
while (m.find()) {
System.out.println(m.group());
}
// Output: 42
Quantifiers: How Many?
| Symbol | Meaning | Example |
|---|---|---|
{3} |
Exactly 3 | a{3} → aaa |
{2,4} |
2 to 4 times | a{2,4} → aa, aaa, aaaa |
{2,} |
2 or more | a{2,} → aa, aaa, aaaa… |
🎪 Common Regex Patterns: Ready-to-Use Magic
Here are patterns you’ll use ALL the time. Like recipes in a cookbook!
đź“§ Email Pattern
String emailRegex =
"^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}quot;;
// Breaking it down:
// ^[\\w.-]+ = starts with letters/numbers
// @ = the @ symbol
// [\\w.-]+ = domain name
// \\. = the dot
// [a-zA-Z]{2,}$ = ends with com, org, etc.
📱 Phone Number Pattern
// Matches: 123-456-7890
String phoneRegex = "\\d{3}-\\d{3}-\\d{4}";
Pattern p = Pattern.compile(phoneRegex);
Matcher m = p.matcher("Call me: 555-123-4567");
if (m.find()) {
System.out.println(m.group());
}
// Output: 555-123-4567
đź”— URL Pattern
String urlRegex =
"https?://[\\w.-]+(/[\\w.-]*)*";
// https? = http or https
// :// = literal ://
// [\\w.-]+ = domain
// (/[\\w.-]*)* = optional path
đź“… Date Pattern (YYYY-MM-DD)
String dateRegex =
"\\d{4}-\\d{2}-\\d{2}";
// \\d{4} = 4 digits (year)
// - = dash
// \\d{2} = 2 digits (month)
// - = dash
// \\d{2} = 2 digits (day)
🔑 Password Validation
// At least 8 chars, 1 uppercase,
// 1 lowercase, 1 digit
String passwordRegex =
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}quot;;
🛠️ String Methods with Regex
Java Strings have built-in methods that use regex. No Pattern/Matcher needed!
matches() - Does It Match Completely?
String text = "Hello123";
// Check if ENTIRE string is letters only
boolean result = text.matches("[a-zA-Z]+");
System.out.println(result); // false
// Now check letters AND numbers
result = text.matches("[a-zA-Z0-9]+");
System.out.println(result); // true
split() - Cut String into Pieces
String sentence = "apple,banana,cherry";
// Split on commas
String[] fruits = sentence.split(",");
// Result: ["apple", "banana", "cherry"]
// Split on any whitespace
String words = "one two three";
String[] parts = words.split("\\s+");
// Result: ["one", "two", "three"]
replaceAll() - Find and Replace
String messy = " too many spaces ";
// Replace multiple spaces with one
String clean = messy.replaceAll("\\s+", " ");
System.out.println(clean);
// Output: " too many spaces "
// Remove all digits
String noNums = "abc123def456".replaceAll(
"\\d", ""
);
// Output: abcdef
replaceFirst() - Replace Only First Match
String text = "cat cat cat";
String result = text.replaceFirst("cat", "dog");
System.out.println(result);
// Output: dog cat cat
🎮 Putting It All Together
Let’s build a simple text validator!
public class TextValidator {
// Patterns for reuse
static Pattern emailPattern =
Pattern.compile(
"^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}quot;
);
static Pattern phonePattern =
Pattern.compile("\\d{3}-\\d{3}-\\d{4}");
public static boolean isValidEmail(String s) {
return emailPattern.matcher(s).matches();
}
public static boolean isValidPhone(String s) {
return phonePattern.matcher(s).matches();
}
public static void main(String[] args) {
System.out.println(
isValidEmail("test@example.com")
); // true
System.out.println(
isValidPhone("555-123-4567")
); // true
}
}
đź§ Quick Reference
graph TD A["Regex in Java"] --> B["Pattern & Matcher"] A --> C["String Methods"] B --> D["compile + matcher + find"] C --> E["matches"] C --> F["split"] C --> G["replaceAll"] C --> H["replaceFirst"]
| Task | Use This |
|---|---|
| Find all matches | Pattern + Matcher + find() |
| Check whole string | String.matches() |
| Split text | String.split() |
| Replace all | String.replaceAll() |
| Replace first | String.replaceFirst() |
🎉 You Did It!
You now have the power to:
- âś… Create Pattern and Matcher objects
- âś… Write regex syntax with special characters
- âś… Use common patterns for emails, phones, dates
- âś… Use String methods with regex
Remember: Regex is like learning a new language. Start simple, practice often, and soon you’ll be writing patterns like a pro! 🚀
Next time you need to find needles in a haystack of text, regex is your magnet!
