简体   繁体   English

Java正则表达式中。(。)的冗余字符转义

[英]Redundant Character Escape of .(dot) in java regex

As far as I know, . 据我所知. is a metacharacter in java regex. 是Java正则表达式中的元字符。 But when I use it as below: 但是当我如下使用它时:

    String s = "1.2.3.4";
    Pattern pattern = Pattern.compile("[\\.]");

I got a Redundant Character Escape warning from IntelliJ IDEA. 我从IntelliJ IDEA收到了“ Redundant Character Escape警告。 Any explaination? 有什么解释吗?

It is because of the square brackets. 这是因为方括号。 Check this: 检查一下:

Pattern pattern = Pattern.compile("[\\.]");
System.out.println(pattern.matcher("").find());
System.out.println(pattern.matcher(".").find());
System.out.println(pattern.matcher("a").find());

false

true 真正

false

pattern = Pattern.compile("[.]");
System.out.println(pattern.matcher("").find());
System.out.println(pattern.matcher(".").find());
System.out.println(pattern.matcher("a").find());

false

true 真正

false

If you were to use the dot outside of the square brackets your escape would be required to capture a dot rather than any character. 如果要在方括号之外使用点,则需要使用转义符来捕获点而不是任何字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM