What is the use of the Pattern.quote method?

I am trying to understand Pattern.quote using the following code:

 String pattern = Pattern.quote("1252343% 8 567 hdfg gf^$545"); System.out.println("Pattern is : "+pattern); 

outputs the result:

 Pattern is : \Q1252343% 8 567 hdfg gf^$545\E 

What is \Q and \E here? The description of the documentation states:

Returns the literal String pattern for the specified String .

This method creates a String that can be used to create a Pattern that matches the string s , as if it were a literal.

Metacharacters or escape sequences in the input sequence will not have special meaning.

But Pattern.quote the return type of the String , not a compiled Pattern .

Why is this method needed and what are some usage examples?

+48
java regex pattern-matching
Mar 14 '13 at 12:27
source share
5 answers

\Q means "start of literal text" (i.e. "open quote" regex)

\E means "end of literal text" (i.e. regex "close quote")

A call to the Pattern.quote() method completes the line in \Q...\E , which turns the text into a regular expression literal. For example, Pattern.quote(".*") Will match a dot and then an asterisk:

 System.out.println("foo".matches(".*")); // true System.out.println("foo".matches(Pattern.quote(".*"))); // false System.out.println(".*".matches(Pattern.quote(".*"))); // true 

The purpose of the method is to not require the programmer to remember the special terms \Q and \E and add a little readability to the code - the regular expression is quite difficult to read. For comparison:

 someString.matches(Pattern.quote(someLiteral)); someString.matches("\\Q" + someLiteral + "\\E")); 

Referring to javadoc :

Returns the string pattern String for the specified string.

This method creates a string that can be used to create a pattern that matches the string s, as if it were an alphabetic pattern.

Metacharacters or escape sequences in the input sequence will not have special meaning.

+62
Mar 14 '13 at 12:30
source share

The Pattern.quote method quotes part of a regular expression pattern to force regex to interpret it as string literals.

Say you have user input in your search program and want to reuse it. But this input may contain unsafe characters, so you can use

 Pattern pattern = Pattern.compile(Pattern.quote(userInput)); 

This method does not quote Pattern , but as you specify, it wraps String in quotation marks of regular expressions.

+16
Mar 14 '13 at 12:32
source share

\Q and \E , among all others, are described in detail on the java.util.regex.Pattern Javadoc page. They mean "begin Q uote", " E nd quote" and mark out the area where all characters have a literal meaning. The way to use the return of Pattern.quote is to pass it to Pattern.compile or any other method that accepts a pattern string, such as String.split .

+9
Mar 14 '13 at 12:30
source share

If you compile the String returned by Pattern.quote , you will get a Pattern that matches the string letter that you quote d.

\Q and \E mark the beginning and end of the quoted part of the line.

+7
Mar 14 '13 at 12:30
source share

Regex often comes across regular strings. Say I want a regular expression to look for a specific string that is known only at run time. How can we be sure that a string does not have a regular expression value, for example ( ".*.*.*" )? We bring it.

+7
Mar 14 '13 at 12:30
source share



All Articles