这些特殊字符在 Java 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11927452/
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
What do these special characters mean in Java?
提问by JourneyMan
I was looking at some of the jdk code. I found these characters. Could someone explain to me what do these mean.
我在看一些 jdk 代码。我找到了这些字符。有人可以向我解释这些是什么意思。
public static String quote(String s) {
int slashEIndex = s.indexOf("\E"); // What does this mean. Is this a special char in java. if so what does this do.
if (slashEIndex == -1)
return "\Q" + s + "\E";
StringBuilder sb = new StringBuilder(s.length() * 2);
sb.append("\Q");
slashEIndex = 0;
int current = 0;
while ((slashEIndex = s.indexOf("\E", current)) != -1) {
sb.append(s.substring(current, slashEIndex));
current = slashEIndex + 2;
sb.append("\E\\E\Q");
}
sb.append(s.substring(current, s.length()));
sb.append("\E");
return sb.toString();
}
From the above code example I was able to figure out what's happening as in the method finds the occurrences of \ and converts them to \E and \Q. Could someone explain why that's the case.
从上面的代码示例中,我能够弄清楚发生了什么,因为在方法中找到了 \ 的出现并将它们转换为 \E 和 \Q。有人可以解释为什么会这样。
For more context on this method, I was looking into the Pattern.quote() method from jdk 1.6
有关此方法的更多上下文,我正在查看 jdk 1.6 中的 Pattern.quote() 方法
采纳答案by Kumar Vivek Mitra
\Q
and \E
is exactly what Pattern.quote()
does, that is to Returns a literal pattern String for the specified String.
\Q
并且\E
正是这样Pattern.quote()
做的,即返回指定字符串的文字模式字符串。
For more details see this link:
有关更多详细信息,请参阅此链接:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
回答by dasblinkenlight
Java regex engine blocks special interpretation of all meta-characters between \Q
and \E
. For example, [name]
matches a single character ('n'
, 'a'
, 'm'
, or 'e'
), while \Q[name]\E
matches six characters - '['
, 'n'
, 'a'
, 'm'
, 'e'
, and ']'
. See the Special Characterssection of the regex tutorialfor more detail.
Java的正则表达式引擎块之间的所有元字符特殊的解释\Q
和\E
。例如,[name]
与单个字符匹配('n'
,'a'
,'m'
,或'e'
),而\Q[name]\E
火柴六个字符- ,'['
,'n'
,'a'
,'m'
,'e'
和']'
。有关更多详细信息,请参阅正则表达式教程的特殊字符部分。
The method makes a regular expression from a string that is presumably provided externally (e.g. entered by a user). Since the string may contain meta-characters, the method encloses the entire string in \Q
and \E
. If the string already contains a \E
, the method inserts the end of the quote, a match of \E
, and a beginning of a new quote for each \E
that it finds..
该方法根据大概是外部提供的(例如,由用户输入的)字符串生成正则表达式。由于字符串可能包含元字符,因此该方法将整个字符串包含在\Q
和 中\E
。如果字符串已经包含 a \E
,则该方法\E
为\E
它找到的每个插入引号的结尾、一个匹配项和一个新引号的开头。
回答by Louis Wasserman
Well, \Q
and \E
have a special meaning in Java regular expressions...
好了,\Q
并\E
在Java中的特殊含义正则表达式...
Most of this method is just working on the tricky edge case of quoting the quote markers \Q
and \E
themselves.
大多数这种方法只是在引用引号\Q
和\E
它们本身的棘手的边缘情况下工作。