正则表达式:如何逃避反斜杠和特殊字符?

时间:2022-10-16 22:25:06

Is there a way to escape ( or protect ) special characters in a regular expression?

有没有办法逃避(或保护)正则表达式中的特殊字符?

What I would like to do is to create a simple regex tester:

我想要做的是创建一个简单的正则表达式测试器:

import java.util.regex.*;
class TestRegex { 
   public static void main( String ... args ) { 
       System.out.printf("%s ~= %s ? %s  %n" , args[0], args[1], Pattern.matches( args[0], args[1] ) );
   }
}

Which works great to test my patterns before plug-in them into the program:

在将模式插入程序之前测试我的模式非常有用:

$java TestRegex "\d" 1
\d ~= 1 ? true  
$java TestRegex "\d" 12
\d ~= 12 ? false  
$java TestRegex "\d+" 12
\d+ ~= 12 ? true  
$java TestRegex "\d+" a12
\d+ ~= a12 ? false  
$java TestRegex "\d+" ""
\d+ ~=  ? false  

The next thing I do is to use this pattern in my program, but each time I have to manually escape it:

我接下来要做的是在我的程序中使用这个模式,但每次我必须手动转义它:

Pattern p = Pattern.compile( /*copy pasted regex here */ );

And in this sample, substitute: \d with \\d. After a while this becomes very irritating .

在此示例中,使用\\ d替换:\ d。过了一会儿,这变得非常恼人。

Q. How can I automatically escape these special characters?

问:如何自动逃避这些特殊字符?

1 个解决方案

#1


26  

You just need to replace all single backslashes with double backslashes. This is complicated a bit since the replaceAll function on String really executes a regular expression and you have to first escape the backslash because it's a literal (yielding \\), and then escape it again because of the regular expression (yielding \\\\). The replacement suffers a similar fate and requires two such escape sequences making it a total of 8 backslashes:

您只需要用双反斜杠替换所有单个反斜杠。这有点复杂,因为String上的replaceAll函数确实执行正则表达式,你必须首先转义反斜杠,因为它是一个文字(产生\\),然后因为正则表达式再次转义它(产生\\\\ )。替换遭受类似的命运,需要两个这样的转义序列,使其总共有8个反斜杠:

System.out.printf("%s ~= %s ? %s  %n", 
    args[0].replaceAll("\\\\","\\\\\\\\"), args[1], ...

#1


26  

You just need to replace all single backslashes with double backslashes. This is complicated a bit since the replaceAll function on String really executes a regular expression and you have to first escape the backslash because it's a literal (yielding \\), and then escape it again because of the regular expression (yielding \\\\). The replacement suffers a similar fate and requires two such escape sequences making it a total of 8 backslashes:

您只需要用双反斜杠替换所有单个反斜杠。这有点复杂,因为String上的replaceAll函数确实执行正则表达式,你必须首先转义反斜杠,因为它是一个文字(产生\\),然后因为正则表达式再次转义它(产生\\\\ )。替换遭受类似的命运,需要两个这样的转义序列,使其总共有8个反斜杠:

System.out.printf("%s ~= %s ? %s  %n", 
    args[0].replaceAll("\\\\","\\\\\\\\"), args[1], ...