Regular Expressions (Regex) in Java provide a powerful way to search, match, and manipulate strings.
They are part of the java.util.regex package and allow you to use patterns to locate, extract, and replace specific text patterns within strings.
In this tutorial, we will cover:
- Basic Usage of Regular Expressions in Java
- Common Regex Patterns and Examples
- Using Pattern and Matcher Classes for Matching
- Search and Replace with Regex
- Splitting Strings Using Regex
Let's explore each section with examples.
Table of Contents
1. Basic Usage of Regular Expressions in Java
Java provides the Pattern and Matcher classes for handling regex operations.
The Pattern class compiles a regex pattern, while the Matcher class applies the pattern to a target string.
Example 1: Basic Matching
import java.util.regex.Pattern; import java.util.regex.Matcher; public class BasicRegexExample { public static void main(String[] args) { String text = "Java is a powerful programming language."; String pattern = "powerful"; // Compile the pattern Pattern compiledPattern = Pattern.compile(pattern); // Create a matcher to search for the pattern Matcher matcher = compiledPattern.matcher(text); if (matcher.find()) { System.out.println("Match found!"); } else { System.out.println("No match found."); } } }
- Explanation: This example checks if the word “powerful” exists in the text string. matcher.find() returns true if a match is found.
2. Common Regex Patterns and Examples
Java regular expressions follow the same basic syntax as other languages.
Here are some common patterns:
Pattern | Description | Example |
---|---|---|
. | Any character | a.c matches “abc”, “adc” |
\d | Any digit (0-9) | \d matches “3” in “123abc” |
\D | Any non-digit | \D matches “a” in “123abc” |
\w | Any word character (a-z, A-Z, 0-9, _) | \w+ matches “abc123” |
\s | Any whitespace character | \s matches space in “a b” |
[abc] | Any character in set | [aeiou] matches “a” in “cat” |
[^abc] | Any character not in set | [^aeiou] matches “c” in “cat” |
`a | b` | Either a or b |
*, +, ? | Zero or more, one or more, zero or one | a*, a+, a? |
{n,m} | Between n and m repetitions | a{2,4} matches “aa”, “aaa” |
Example 2: Using Common Patterns to Match Strings
import java.util.regex.Pattern; import java.util.regex.Matcher; public class PatternExample { public static void main(String[] args) { String text = "Contact us at info@example.com or visit www.example.com"; String emailPattern = "\\w+@\\w+\\.\\w+"; // Email pattern String urlPattern = "www\\.\\w+\\.\\w+"; // URL pattern // Match email Matcher emailMatcher = Pattern.compile(emailPattern).matcher(text); if (emailMatcher.find()) { System.out.println("Email found: " + emailMatcher.group()); } // Match URL Matcher urlMatcher = Pattern.compile(urlPattern).matcher(text); if (urlMatcher.find()) { System.out.println("URL found: " + urlMatcher.group()); } } }
Output:
Email found: info@example.com URL found: www.example.com
- Explanation: The email pattern \\w+@\\w+\\.\\w+ matches a basic email format, and the URL pattern www\\.\\w+\\.\\w+ matches web addresses starting with www.
3. Using Pattern and Matcher Classes for Matching
The Pattern and Matcher classes provide methods like matches(), find(), and group() for working with regular expressions.
Example 3: Extracting Groups with Pattern and Matcher
import java.util.regex.Pattern; import java.util.regex.Matcher; public class GroupExample { public static void main(String[] args) { String text = "User: JohnDoe, Email: john.doe@example.com"; String pattern = "(\\w+): (\\w+), Email: (\\w+\\.\\w+@\\w+\\.\\w+)"; Matcher matcher = Pattern.compile(pattern).matcher(text); if (matcher.find()) { System.out.println("Username: " + matcher.group(2)); System.out.println("Email: " + matcher.group(3)); } } }
Output:
Username: JohnDoe Email: john.doe@example.com
- Explanation: Using parentheses in the pattern (…) creates capturing groups. matcher.group(2) extracts the username, and matcher.group(3) extracts the email.
4. Search and Replace with Regex
The replaceAll() method allows you to replace matched patterns with a specific string.
Example 4: Replacing Words in a String
public class ReplaceExample { public static void main(String[] args) { String text = "Java is great. Java is powerful."; String updatedText = text.replaceAll("Java", "Python"); System.out.println("Updated text: " + updatedText); } }
Output:
Updated text: Python is great. Python is powerful.
- Explanation: This example replaces all occurrences of “Java” with “Python” using replaceAll().
Example 5: Using Regex in Replace to Format a String
public class FormatExample { public static void main(String[] args) { String text = "UserID123"; String formattedText = text.replaceAll("(UserID)(\\d+)", "$1-#$2"); System.out.println("Formatted text: " + formattedText); } }
Output:
Formatted text: UserID-#123
- Explanation: Using replaceAll() with () captures groups in the pattern and replaces them in the output. $1 and $2 reference the groups.
5. Splitting Strings Using Regex
You can split a string into parts based on a regex pattern with split().
Example 6: Splitting a String by Multiple Delimiters
import java.util.Arrays; public class SplitExample { public static void main(String[] args) { String text = "apple,orange;banana|grape"; String[] fruits = text.split("[,;|]"); System.out.println("Fruits: " + Arrays.toString(fruits)); } }
Output:
Fruits: [apple, orange, banana, grape]
- Explanation: The pattern [ ,;|] matches commas, semicolons, or vertical bars, and split() divides the string based on any of these delimiters.
Example 7: Splitting a String by Whitespace and Trimming Extra Spaces
public class SplitTrimExample { public static void main(String[] args) { String text = " Java is powerful "; String[] words = text.trim().split("\\s+"); for (String word : words) { System.out.println(word); } } }
Output:
Java is powerful
- Explanation: The split(“\\s+”) pattern splits by one or more whitespace characters, and trim() removes leading and trailing spaces.
Summary of Regular Expressions in Java
Regex Function | Description |
---|---|
Pattern.compile() | Compiles a regex pattern. |
matcher() | Creates a Matcher instance for applying the pattern. |
find() | Searches for the next match of the pattern in the text. |
matches() | Checks if the entire text matches the regex pattern. |
group() | Returns matched groups from the pattern using capturing groups. |
replaceAll() | Replaces all matches in the text with a specified replacement. |
split() | Splits text into an array based on the regex pattern. |
Conclusion
In this tutorial, we explored various ways to use regular expressions in Java, including:
- Basic pattern matching with Pattern and Matcher.
- Common regex patterns for matching strings.
- Extracting groups from matches.
- Using regex to search and replace text.
- Splitting text based on complex patterns.