java pattern.quote,Pattern.quote方法有什么用?

I'm 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);

produces the output:

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

What are \Q and \E here? The documentation description says :

Returns a literal pattern String for the specified String.

This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.

Metacharacters or escape sequences in the input sequence will be given no special meaning.

But Pattern.quote's return type is String and not a compiled Pattern object.

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

解决方案

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

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

Calling the Pattern.quote() method wraps the string in \Q...\E, which turns the text is into a regex literal. For example, Pattern.quote(".*") would 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 method's purpose is to not require the programmer to have to remember the special terms \Q and \E and to add a bit of readability to the code - regex is hard enough to read already. Compare:

someString.matches(Pattern.quote(someLiteral));

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

Referring to the javadoc:

Returns a literal pattern String for the specified String.

This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.

Metacharacters or escape sequences in the input sequence will be given no special meaning.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值