Regular expressions (RegEx) are a powerful tool for pattern matching and text manipulation in programming. In Java, the Pattern and Matcher classes provide comprehensive support for working with regular expressions. This tutorial will guide you through the basics of regex syntax, how to use the Pattern and Matcher classes, and advanced features like replaceAll.
Regular expressions are sequences of characters that define a search pattern. They are widely used in text processing tasks such as searching, replacing, splitting, and validating strings. In Java, the java.util.regex package provides the necessary tools to work with regex.
Understanding regular expressions is crucial for developers who need to perform complex string manipulations or validations in their applications. Whether you're building a search engine, data validator, or text formatter, regex can simplify your code significantly.
The Pattern class represents a compiled regular expression, while the Matcher class is used to match this pattern against input strings. Here's how they work together:
Pattern.compile() method to compile a regex string into a Pattern object.pattern.matcher(input) method to create a Matcher object that will search the input string for matches of the pattern.1import java.util.regex.*;23public class BasicRegex {4public static void main(String[] args) {5String regex = "hello";6String input = "Hello, world!";78// Compile the regular expression9Pattern pattern = Pattern.compile(regex);1011// Create a matcher for the input string12Matcher matcher = pattern.matcher(input);1314// Check if the input contains the pattern15boolean found = matcher.find();16System.out.println("Found: " + found);17};18}
Found: false
In this example, the regex "hello" is case-sensitive. Since the input string "Hello, world!" starts with a capital 'H', the match fails.
Regular expressions in Java follow a syntax similar to other programming languages like Perl and JavaScript. Here are some common regex constructs:
| Construct | Description |
|---|---|
. | Matches any single character (except newline) |
* | Matches zero or more occurrences of the preceding element |
+ | Matches one or more occurrences of the preceding element |
? | Matches zero or one occurrence of the preceding element |
[] | Character class; matches any single character inside the brackets |
[^] | Negated character class; matches any single character not inside the brackets |
() | Grouping; treats the enclosed elements as a single unit |
1import java.util.regex.*;23public class AdvancedRegex {4public static void main(String[] args) {5String regex = "h.*o";6String input = "Hello, world!";78// Compile the regular expression9Pattern pattern = Pattern.compile(regex);1011// Create a matcher for the input string12Matcher matcher = pattern.matcher(input);1314// Check if the input contains the pattern15boolean found = matcher.find();16System.out.println("Found: " + found);17}18}
Found: true
In this example, the regex "h.*o" matches any string that starts with 'h', followed by zero or more characters, and ends with 'o'. The input string "Hello, world!" satisfies this pattern.
The Matcher class provides various methods for manipulating strings based on the matched patterns. One of the most useful methods is replaceAll(), which replaces all occurrences of the pattern in the input string with a specified replacement string.
1import java.util.regex.*;23public class ReplaceAllExample {4public static void main(String[] args) {5String regex = "world";6String input = "Hello, world!";7String replacement = "Java";89// Compile the regular expression10Pattern pattern = Pattern.compile(regex);1112// Create a matcher for the input string13Matcher matcher = pattern.matcher(input);1415// Replace all occurrences of the pattern16String result = matcher.replaceAll(replacement);17System.out.println("Result: " + result);18}19}
Result: Hello, Java!
In this example, the regex "world" matches the word "world" in the input string. The replaceAll() method replaces it with "Java", resulting in the output "Hello, Java!".
Let's create a practical example that demonstrates how to use regular expressions to validate email addresses and replace them if necessary.
1import java.util.regex.*;23public class EmailValidator {4public static void main(String[] args) {5String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}";6String input = "Contact us at info@example.com or support@sample.org";7String replacement = "contact@codingstuff.io";89// Compile the regular expression10Pattern pattern = Pattern.compile(regex);1112// Create a matcher for the input string13Matcher matcher = pattern.matcher(input);1415// Check if the input contains valid emails16while (matcher.find()) {17System.out.println("Found email: " + matcher.group());18}1920// Replace all occurrences of the pattern21String result = matcher.replaceAll(replacement);22System.out.println("Result: " + result);23}24}
Found email: info@example.com Found email: support@sample.org Result: Contact us at contact@codingstuff.io or contact@codingstuff.io
In this example, the regex "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}" matches valid email addresses. The program first prints all found emails and then replaces them with "contact@codingstuff.io".
| Key Concept | Description |
|---|---|
| Pattern | Represents a compiled regular expression |
| Matcher | Used to match the pattern against input strings |
| replaceAll | Replaces all occurrences of the pattern in the input string |
Pattern and Matcher classes provide the necessary tools to work with regex in Java.replaceAll() method is useful for replacing matched patterns in strings.Now that you have a solid understanding of regular expressions in Java, it's time to explore concurrency and multithreading. In the next tutorial, we'll dive into how to create and manage threads in Java, enabling your applications to perform multiple tasks simultaneously. Stay tuned!