The Pattern class

The Pattern class represents the compiled form of a string regular expression. So far, we have provided all the regular expressions as strings. Each String regular expression must be compiled into an instance of the Pattern class before the Java regex engine can execute it. An instance of the Pattern class is used to create a Matcher object to match input text against the regular expression.

Let's list down the important and useful methods from the Pattern class first:

Method Signature

Description

Static Pattern compile(String regex)

Compiles the given String regular expression into a Pattern instance.

Static Pattern compile(String regex, int flags)

Compiles the given String regular expression into a Pattern instance with the given flags. Flags can be one or more of DOTALL, MULTILINE, CASE_SENSITIVE, UNICODE_CHARACTER_CLASS and a few others. Check the Java Pattern API at http://download.java.net/java/jdk9/docs/api/java/util/regex/Pattern.html for all flags and their descriptions.

Matcher matcher(CharSequence input)

Creates a matcher instance to match the given input against this compiled pattern.

String quote(String str)

Returns a literal pattern string for the specified string. After quoting the String, regex meta characters or escape sequences in the input string will just be literals without any special meaning. Pattern.quote wraps the given string in \Q and \E escape constructs. These special escape constructs are used to make a wrapped string as a literal string, thus removing all the special meanings of regex meta and special characters.

Predicate<String>

asPredicate ()

Predicate<String> asPredicate ()

Creates a Predicate of a string to match the input string.

Stream<String> splitAsStream(CharSequence input)

Splits the given input string using this pattern and creates a stream from the given input sequence around the matches of this pattern (added in Java 8).

String[] split(CharSequence input)

Splits the given input sequence around the matches of this pattern. It is the same as String.split(String regex).

String[] split(CharSequence input, int limit)

Splits the given input sequence around the matches of this pattern. The limit parameter controls the number of times the pattern is applied and, therefore, affects the length of the resulting array. It is the same as String.split(String regex, int limit).

 

The Pattern class has a static method that can be called to match a string against a regular expression. This is as follows:

boolean matches(String regex, CharSequence input) 

It can be used instead of the following:

final Pattern p = Pattern.compile(regex); 
Matcher m = p.matcher(input);
m.matches();

This is actually the JDK9 implementation of this method. Although it is simpler and shorter to call this method instead of three lines, it is recommended to use the methods compile(), matcher(), and matches() separately if we perform matching against the same regular expression many times. In such a case, we can invoke compile() only the first time and keep the compiled pattern and avoid recompilation each time the matching is performed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.138