正则表达式:如何逃避反斜杠和特殊字符? [英] regex: How to escape backslashes and special characters?
问题描述
有没有办法逃避(或保护)正则表达式中的特殊字符?
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 */ );
在此示例中,替换为: \d
使用 \\\\
。过了一会儿,这变得非常恼人。
And in this sample, substitute: \d
with \\d
. After a while this becomes very irritating .
问:如何自动转义这些特殊字符?
Q. How can I automatically escape these special characters?
推荐答案
您只需要用双反斜杠替换所有单个反斜杠。这有点复杂,因为 String
上的 replaceAll
函数确实执行正则表达式而你必须首先转义反斜杠因为它是一个文字(产生 \\
),然后因为正则表达式再次转义它(产生 \\\\\ \\
)。替换遭受类似的命运,需要两个这样的转义序列,使其总共有8个反斜杠:
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:
System.out.printf("%s ~= %s ? %s %n",
args[0].replaceAll("\\\\","\\\\\\\\"), args[1], ...
这篇关于正则表达式:如何逃避反斜杠和特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!