Java 如何为模式编译转义方括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1140268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to escape a square bracket for Pattern compilation
提问by Afamee
I have comma separated list of regular expressions:
我有逗号分隔的正则表达式列表:
.{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]
I have done a split on the comma. Now I'm trying to match this regex against a generated password. The problem is that Pattern.compile
does not like square brackets that is not escaped. Can some please give me a simple function that takes a string like so: [0-9]
and returns the escaped string \[0-9\]
.
我对逗号做了拆分。现在我正在尝试将此正则表达式与生成的密码进行匹配。问题是Pattern.compile
不喜欢没有转义的方括号。有人可以给我一个简单的函数,它接受一个像这样的字符串:[0-9]
并返回转义的 string \[0-9\]
。
采纳答案by Laurence Gonsalves
You can use Pattern.quote(String)
.
您可以使用Pattern.quote(String)
.
From the docs:
从文档:
public static String quote?(String s)
Returns a literal pattern
String
for the specifiedString
.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.
public static String quote?(String s)
返回
String
指定的文字模式String
。此方法生成一个字符串,可用于创建一个模式,该模式将匹配字符串 s,就像它是文字模式一样。
输入序列中的元字符或转义序列将没有特殊含义。
回答by Dan Breen
You can use the \Q and \E special characters...anything between \Q and \E is automatically escaped.
您可以使用 \Q 和 \E 特殊字符... \Q 和 \E 之间的任何内容都会自动转义。
\Q[0-9]\E
回答by Alan Moore
Pattern.compile()
likes square brackets just fine. If you take the string
Pattern.compile()
喜欢方括号就好了。如果你把字符串
".{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]"
and split it on commas, you end up with five perfectly valid regexes: the first one matches eight non-line-separator characters, the second matches an ASCII digit, and so on. Unless you really want to match strings like ".{8}"
and "[0-9]"
, I don't see why you would need to escape anything.
并用逗号分割它,你最终得到五个完全有效的正则表达式:第一个匹配八个非行分隔符,第二个匹配一个 ASCII 数字,依此类推。除非你真的想匹配像".{8}"
and"[0-9]"
这样的字符串,否则我不明白你为什么需要转义任何东西。
回答by Cullub
For some reason, the above answer didn't work for me. For those like me who come after, here is what I found.
出于某种原因,上述答案对我不起作用。对于像我这样追求的人,这就是我的发现。
I was expecting a single backslash to escape the bracket, however, you must use two if you have the pattern stored in a string. The first backslash escapes the second one into the string, so that what regex sees is \]
. Since regex just sees one backslash, it uses it to escape the square bracket.
我希望用一个反斜杠来转义括号,但是,如果您将模式存储在字符串中,则必须使用两个反斜杠。第一个反斜杠将第二个反斜杠转义到字符串中,因此正则表达式看到的是\]
. 由于正则表达式只看到一个反斜杠,它使用它来转义方括号。
\]
In regex, that will match a single closing square bracket.
在正则表达式中,这将匹配单个右方括号。
If you're trying to match a newline, for example though, you'd only use a single backslash. You're using the string escape pattern to insert a newline character into the string. Regex doesn't see \n
- it sees the newline character, and matches that. You need two backslashes because it's not a string escape sequence, it's a regex escape sequence.
例如,如果您尝试匹配换行符,则只能使用一个反斜杠。您正在使用字符串转义模式将换行符插入到字符串中。正则表达式看不到\n
- 它看到换行符,并与之匹配。您需要两个反斜杠,因为它不是字符串转义序列,而是正则表达式转义序列。