Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the Pattern
class and the Matcher
class are part of the java.util.regex
package, and they provide support for regular expressions. Regular expressions are used to perform advanced text manipulation, such as searching, extracting, and validating text based on patterns. In this tutorial, we will cover the basics of the Pattern
and Matcher
classes in Java.
To create a Pattern
object, use the static Pattern.compile()
method, which takes a regular expression pattern string as its argument.
Example:
import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("\\d+"); } }
In this example, we create a Pattern
object that matches one or more digits (\\d+
).
To create a Matcher
object, call the matcher()
method on a Pattern
object, passing the input string as an argument.
Example:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher("123 abc 456"); } }
The Matcher
class provides several methods to perform operations on the input string based on the pattern. Some common methods include:
find()
: Searches for the next occurrence of the pattern in the input string.group()
: Returns the matched text.start()
: Returns the start index of the matched text.end()
: Returns the end index of the matched text.Example:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher("123 abc 456"); while (matcher.find()) { System.out.println("Matched text: " + matcher.group()); System.out.println("Start index: " + matcher.start()); System.out.println("End index: " + matcher.end()); } } }
Output:
Matched text: 123 Start index: 0 End index: 3 Matched text: 456 Start index: 8 End index: 11
matches()
: Returns true
if the entire input string matches the pattern, false
otherwise.replaceFirst(String replacement)
: Replaces the first occurrence of the pattern in the input string with the given replacement.replaceAll(String replacement)
: Replaces all occurrences of the pattern in the input string with the given replacement.Example:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher("123 abc 456"); System.out.println("Matches: " + matcher.matches()); System.out.println("Replace first: " + matcher.replaceFirst("XXX")); System.out.println("Replace all: " + matcher.replaceAll("XXX")); } }
Output:
Matches: false Replace first: XXX abc 456 Replace all: XXX abc XXX
In this tutorial, we covered the basics of the Pattern
and Matcher
classes in Java, which are part of the java.util.regex
package. These classes provide powerful functionality for working with regular expressions, enabling you to search, extract, and manipulate text based on patterns.
Java Pattern Class Example Code:
Pattern
class compiles regex patterns into a Pattern
object for later use with the Matcher
.import java.util.regex.Pattern; public class PatternExample { public static void main(String[] args) { Pattern pattern = Pattern.compile("ab+c"); // Use the pattern with Matcher... } }
Matching Patterns in Strings using Java:
Matcher
class is used to match a Pattern
against a given input string.import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample { public static void main(String[] args) { Pattern pattern = Pattern.compile("ab+c"); Matcher matcher = pattern.matcher("abbbc"); boolean isMatch = matcher.matches(); System.out.println("Is match: " + isMatch); } }
Java Matcher Class Methods and Usage:
Matcher
class provides methods like matches()
, find()
, group()
, etc., for working with matches.Matcher matcher = pattern.matcher("abbbc"); if (matcher.find()) { String matchedText = matcher.group(); System.out.println("Matched text: " + matchedText); }
Pattern.compile() Method in Java:
Pattern.compile()
method is used to compile a regular expression pattern into a Pattern
object.Pattern pattern = Pattern.compile("[0-9]+");
Grouping and Capturing in Java Regular Expressions:
()
are used for grouping and capturing parts of a regex pattern.Pattern pattern = Pattern.compile("(\\d+)-(\\d+)"); Matcher matcher = pattern.matcher("123-456"); if (matcher.find()) { String group1 = matcher.group(1); String group2 = matcher.group(2); System.out.println("Group 1: " + group1 + ", Group 2: " + group2); }
Java Pattern Flags and Modifiers:
Pattern pattern = Pattern.compile("abc", Pattern.CASE_INSENSITIVE);
Matching Multiple Patterns with Java Pattern and Matcher:
|
(pipe) operator to match multiple patterns.Pattern pattern = Pattern.compile("cat|dog"); Matcher matcher = pattern.matcher("I have a cat");
Java Pattern Split Method:
split()
method in Pattern
class is used to split a string based on a regex pattern.String[] parts = pattern.split("apple,orange,banana");
Java Matcher Find Method:
find()
method in Matcher
class is used to find the next subsequence of the input sequence that matches the pattern.while (matcher.find()) { // Process each match }
Java Matcher replaceFirst and replaceAll Methods:
replaceFirst()
and replaceAll()
methods are used to replace the first or all occurrences of the matched pattern in the input string.String replaced = matcher.replaceAll("replacement");
Common Regex Patterns and Examples in Java:
Pattern emailPattern = Pattern.compile("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b");