How to Use Regex in Java: Syntax, Examples, and Tips
In Java, you use regex by importing
java.util.regex.Pattern and java.util.regex.Matcher. You create a Pattern object with your regex, then use a Matcher to find matches in text.Syntax
To use regex in Java, you first create a Pattern object with your regex string. Then, you create a Matcher object by calling pattern.matcher(inputString). Use matcher.find() or matcher.matches() to check for matches.
- Pattern.compile(regex): Compiles the regex into a pattern.
- pattern.matcher(text): Creates a matcher for the text.
- matcher.find(): Finds the next match.
- matcher.matches(): Checks if the entire text matches the pattern.
java
import java.util.regex.Pattern; import java.util.regex.Matcher; Pattern pattern = Pattern.compile("your-regex"); Matcher matcher = pattern.matcher("input text"); if (matcher.find()) { // match found } if (matcher.matches()) { // entire input matches }
Example
This example shows how to check if a string contains a number using regex. It prints all numbers found in the text.
java
import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexExample { public static void main(String[] args) { String text = "I have 2 apples and 10 bananas."; Pattern pattern = Pattern.compile("\\d+"); // regex for one or more digits Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println("Found number: " + matcher.group()); } } }
Output
Found number: 2
Found number: 10
Common Pitfalls
Common mistakes include forgetting to escape special characters in regex strings, confusing matches() with find(), and not using raw strings properly.
Wrong: Using matches() when you want to find a substring match.
Right: Use find() to search for a pattern anywhere in the text.
java
import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexPitfall { public static void main(String[] args) { String text = "abc123def"; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(text); // Wrong: matches() checks whole string System.out.println("Using matches(): " + matcher.matches()); // false // Right: find() checks for substring match System.out.println("Using find(): " + matcher.find()); // true } }
Output
Using matches(): false
Using find(): true
Quick Reference
| Method | Description |
|---|---|
| Pattern.compile(regex) | Creates a regex pattern object |
| pattern.matcher(text) | Creates a matcher for the text |
| matcher.find() | Finds next substring matching the pattern |
| matcher.matches() | Checks if entire text matches pattern |
| matcher.group() | Returns the matched substring |
| matcher.start() | Returns start index of match |
| matcher.end() | Returns end index of match |
Key Takeaways
Use Pattern.compile() to create a regex pattern in Java.
Use Matcher to search text with methods like find() and matches().
Escape special regex characters properly in Java strings.
Use find() to locate substrings, matches() to check full string match.
Use matcher.group() to get the matched text after a successful find().