Regular Expressions – Regex in Java
Regular expressions (regex) in Java are a powerful tool for pattern matching and text manipulation. They provide a flexible and concise way to search, extract, and manipulate text based on specific patterns. In this article, we will explore regex in Java, their syntax, and provide code examples to illustrate their usage.
1. Introduction to Regular Expressions
Regular expressions are a sequence of characters that define a search pattern. They are commonly used for pattern matching and text manipulation tasks, such as searching for specific strings, validating input, and data extraction.
2. Syntax and Basic Patterns
Regular expressions use a combination of characters and special metacharacters to define patterns. Here are some common metacharacters:
- . (Period): Matches any single character except a newline.
- * (Asterisk): Matches zero or more occurrences of the preceding character or group.
- + (Plus): Matches one or more occurrences of the preceding character or group.
- ? (Question Mark): Matches zero or one occurrence of the preceding character or group.
3. Using Regex in Java
Java provides the java.util.regex
package for working with regular expressions. You can use the Pattern
and Matcher
classes to perform pattern matching. Let’s look at an example:
Java Regex Example:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String text = "The quick brown fox jumps over the lazy dog.";
String pattern = "fox";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text);
if (matcher.find()) {
System.out.println("Match found: " + matcher.group());
} else {
System.out.println("No match found.");
}
}
}
In this example, we create a regular expression pattern that matches the word “fox” in the given text. The Matcher
class is used to find and extract matching substrings. If a match is found, it is displayed; otherwise, a message is printed.
4. Common Use Cases
Regular expressions are versatile and find applications in various use cases:
- Form Validation: Validate email addresses, phone numbers, or other user inputs.
- Text Search and Replace: Search for specific text patterns and replace them with desired content.
- Data Extraction: Extract data from unstructured text, such as log files or web pages.
- URL Routing: Match and route URLs to the appropriate handlers in web applications.
5. Character Classes and Ranges
Character classes allow you to match a set of characters. You can define custom character ranges or use predefined character classes like \d
for digits, \w
for word characters, and \s
for whitespace. Here’s an example:
String text = "The code examples are 123 and A45.";
String pattern = "[0-9A-Z]";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text);
while (matcher.find()) {
System.out.println("Match found: " + matcher.group());
}
This code matches and prints all alphanumeric characters in the given text.
6. Quantifiers and Groups
Quantifiers are used to specify how many times a character or group should be matched. Groups allow you to group multiple characters or subpatterns together. For example:
String text = "The price is $19.99, but the discount is $5.50.";
String pattern = "\\$\\d+\\.\\d+";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text);
while (matcher.find()) {
System.out.println("Match found: " + matcher.group());
}
This code matches and prints all monetary values in the text, such as “$19.99” and “$5.50.”
7. Conclusion
Regular expressions are a valuable tool for pattern matching and text manipulation in Java. By understanding their syntax and using the Java Pattern
and Matcher
classes, you can perform a wide range of tasks, from basic pattern matching to complex text extraction and validation.