简体   繁体   English

如何在正则表达式中转义字符

[英]How to escape characters in a regular expression

When I use the following code I've got an error: 当我使用以下代码时,出现错误:

 Matcher matcher = pattern.matcher("/Date\(\d+\)/");

The error is : 错误是:

invalid escape sequence (valid ones are \\b \\t \\n \\f \\r \\" \\' \\\\ )

I have also tried to change the value in the brackets to ('/Date\\(\\d+\\)/') ; 我也尝试将方括号中的值更改为('/Date\\(\\d+\\)/') ; without any success. 没有任何成功。

How can i avoid this error? 如何避免此错误?

You need to double-escape your \\ character, like this: \\\\ . 您需要两次转义\\字符,例如: \\\\

Otherwise your String is interpreted as if you were trying to escape ( . 否则,您的String被解释为您试图转义(

Same with the other round bracket and the d. 与另一个圆括号和d相同。

In fact it seems you are trying to initialize a Pattern here, while pattern.matcher references a text you want your Pattern to match. 实际上,您似乎在这里尝试初始化一个Pattern ,而pattern.matcher引用了一个您希望您的Pattern匹配的文本。

Finally, note that in a Pattern , escaped characters require a double escape, as such: 最后,请注意,在Pattern ,转义字符需要两次转义,如下所示:

\\(\\d+\\)

Also, as Rohit says, Pattern s in Java do not need to be surrounded by forward slashes ( / ). 而且,正如Rohit所说,Java中的Pattern不需要被正斜杠( / )包围。

In fact if you initialize a Pattern like that, it will interpret your Pattern as starting and ending with literal forward slashes. 实际上,如果您这样初始化一个Pattern ,它将把您的Pattern解释为以文字正斜杠开始和结束。

Here's a small example of what you probably want to do: 这是您可能想做的一个小例子:

// your input text
String myText = "Date(123)";
// your Pattern initialization
Pattern p = Pattern.compile("Date\\(\\d+\\)");
// your matcher initialization
Matcher m = p.matcher(myText);
// printing the output of the match...
System.out.println(m.find());

Output: 输出:

true

Your regex is correct by itself , but in Java, the backslash character itself needs to be escaped. 您的正则表达式本身是正确 ,但是在Java中,反斜杠字符本身需要转义。

Thus, this regex: 因此,此正则表达式:

/Date\(\d+\)/

Must turn into this: 必须变成这样:

/Date\\(\\d+\\)/

One backslash is for escaping the parenthesis or d . 一个反斜杠用于转义括号或d The other one is for escaping the backslash itself. 另一个是用于转义反斜杠本身。

The error message you are getting arises because Java thinks you're trying to use \\( as a single escape character, like \\n , or any of the other examples. However, \\( is not a valid escape sequence, and so Java complains. 由于Java认为您正在尝试将\\(作为单个转义字符,如\\n或任何其他示例,因此会出现错误消息。但是, \\(不是有效的转义序列,因此Java抱怨。


In addition, the logic of your code is probably incorrect. 另外,代码的逻辑可能不正确。 The argument to matcher should be the text to search (for example, "/Date(234)/Date(6578)/"), whereas the variable pattern should contain the pattern itself. matcher的参数应该是要搜索的文本(例如,“ / Date(234)/ Date(6578)/”),而可变pattern应该包含模式本身。 Try this: 尝试这个:

String textToMatch = "/Date(234)/Date(6578)/";
Pattern pattern = pattern.compile("/Date\\(\\d+\\)/");
Matcher matcher = pattern.matcher(textToMatch);

Finally, the regex character class \\d means "one single digit." 最后,正则表达式字符类\\d表示“一位数字”。 If you are trying to refer to the literal phrase \\\\d , you would have to use \\\\\\\\d to escape this. 如果尝试引用文字短语\\\\d ,则必须使用\\\\\\\\d来对此进行转义。 However, in that case, your regex would be a constant, and you could use textToMatch.indexOf and textToMatch.contains more easily. 但是,在这种情况下,您的正则表达式将是一个常量,并且可以更轻松地使用textToMatch.indexOftextToMatch.contains

要在Java中转义正则表达式,还可以使用Pattern.quote()

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

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