Java Regular Expressions, commonly referred to as Java RegEx, is a powerful tool used for pattern matching and text processing.
It allows you to search for specific patterns in strings and manipulate the matching results. RegEx is a sequence of characters that defines a search pattern.
Java has built-in support for regular expressions through the java.util.regex package, which provides several classes and methods to perform RegEx operations.
Basic Syntax
The basic syntax of a regular expression is as follows:
String pattern = "expression";
Here, the expression is a string that defines the pattern to be searched for in the target string.
Special Characters
The following are some special characters used in Java RegEx:
- . (dot) - Matches any single character except a line terminator
- * - Matches zero or more occurrences of the preceding character or group
- +? - Matches one or more occurrences of the preceding character or group
- ? - Matches zero or one occurrence of the preceding character or group
- ^ - Matches the start of a line
- $ - Matches the end of a line
- [ ] - Matches any character within the square brackets
- {n} - Matches exactly n occurrences of the preceding character or group
- {n,} - Matches n or more occurrences of the preceding character or group
- {n,m} - Matches at least n and at most m occurrences of the preceding character or group
- | (pipe) - Matches either the preceding or the following character or group
- ( ) - Groups characters and captures the matched text
- \ - Escapes a special character
Java RegEx Classes and Methods
The java.util.regex package provides several classes and methods to perform RegEx operations:
Pattern Class:
Compiles a regular expression into a pattern. It provides the compile() method to compile a RegEx expression into a pattern.
String patternString = "expression";
Pattern pattern = Pattern.compile(patternString);
Matcher Class:
Performs match operations on a target string using a pattern.
It provides several methods, including the following:
- matches() - Attempts to match the entire input sequence against the pattern.
- lookingAt() - Attempts to match the input sequence, starting at the beginning, against the pattern.
- find() - Scans the input sequence looking for the next subsequence that matches the pattern.
String input = "target_string";
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// matching code
}
PatternSyntaxException:
Thrown to indicate a syntax error in a regular expression pattern.
Matching and Replacing Text
The following code example shows how to use the Java RegEx classes and methods to match and replace text in a target string:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExExample {
public static void main(String[] args) {
String patternString = "dog";
String input = "The quick brown dog jumps over the lazy dog.";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(input);
// match and replace text
String output = matcher.replaceAll("cat");
System.out.println(output);
}
}
Output:
The quick brown cat jumps over the lazy cat.
In this example, the patternString variable is set to dog, which is the pattern we want to search for in the target string.
The input variable is set to "The quick brown dog jumps over the lazy dog.", which is the target string.
The compile() method is used to compile the pattern string into a pattern object.
The matcher() method is used to create a Matcher object that performs match operations on the target string using the pattern.
Finally, the replaceAll() method is used to replace all occurrences of the pattern in the target string with the string "cat".
The result is then printed to the console, showing that all occurrences of the word "dog" in the target string have been replaced with the word "cat".
Conclusion
Java RegEx is a powerful tool for pattern matching and text processing.
It provides several classes and methods to perform RegEx operations and can be used to match and replace text in target strings.
Understanding the basic syntax, special characters, and classes and methods of Java RegEx is essential for using it effectively.