Pattern Class
Package: java.util.regex
The Pattern class represents a regular expression that has been compiled into executable form. Pattern objects are used in conjunction with the Matcher class, which lets you match strings to a compiled Pattern object.
The Pattern class doesn’t have a constructor. Instead, you create an instance of the Pattern class by calling the compile method.
Methods
|
Method |
What It Does |
|
|
Compiles the specified pattern. This static method returns a |
|
|
Creates a |
|
|
Compiles the specified pattern, and then attempts to match it to the specified input string. |
|
|
Creates an array of strings that are split from the input string using the compiled pattern. |
Here’s an example that matches a simple pattern against an input string:
String regex = “b{aeiou}t”;
Pattern p = Pattern.compile(regex);
String input = “bat”;
bool result = p.matches(regex, input); // true
Here’s a similar example that uses the matcher method to return a Matcher object, which is then used to match the string:
String regex = “b{aeiou}t”;
Pattern p = Pattern.compile(regex);
String input = “bat”;
Matcher m = p.matcher(input);
bool ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access