为什么这个Java regex会导致“非法转义字符”错误?

时间:2022-12-14 09:08:58

I'm working on a solution to a previous question, as best as I can, using regular expressions. My pattern is

我正在使用正则表达式,尽我所能地解决上一个问题的解决方案。我的模式

"\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

According to NetBeans, I have two illegal escape characters. I'm guessing it has to do with the \d and \w, but those are both valid in Java. Perhaps my syntax for a Java regular expression is off...

根据NetBeans的说法,我有两个非法转义字符。我猜它和\d和\w有关,但是它们都是在Java中有效的。也许我的Java正则表达式的语法是off…

The entire line of code that is involved is:

涉及的整个代码行是:

userTimestampField = new FormattedTextField(
  new RegexFormatter(
    "\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"
));

6 个解决方案

#1


49  

Assuming this regex is inside a Java String literal, you need to escape the backslashes for your \d and \w tags:

假设这个正则表达式位于Java字符串的字面意思中,那么您需要为您的\d和\w标记摆脱反斜杠:

"\\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

This gets more, well, bonkers frankly, when you want to match backslashes:

坦白地说,当你想要匹配反斜杠时,这种情况会变得更糟:

public static void main(String[] args) {        
    Pattern p = Pattern.compile("\\\\\\\\"); //ERM, YEP: 8 OF THEM
    String s = "\\\\";
    Matcher m = p.matcher(s);
    System.out.println(s);
    System.out.println(m.matches());
}

\\ //JUST TO MATCH TWO SLASHES :(
true

#2


6  

What about the following: \\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

关于以下:\ \ d { 3 } { 4 } \ \ w(0(1 - 9)|[12][0 - 9]| 3[01])([01][0 - 9]| 2(0 - 3))([0 - 9][0 - 5]){ 2 }

#3


1  

Have you tried this?

你有试过吗?

\\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

#4


0  

Did you try "\\d" and "\\w"?

你试过“\\”和“\\w”吗?

-edit- Lol I posted the right answer and get down voted and then I notice that * escapes backslashes so my answer appeared wrong :)

-编辑- Lol我发布了正确的答案,然后投票,然后我注意到*逃回了反斜杠,所以我的答案出现了错误:)

#5


0  

all you need to do is to put

你所要做的就是把它放进去。

 *\
 ex: string ex = 'this is the character: *\\s';

before your invalid character and not 8 \ !!!!!

在您的无效字符之前,而不是8 \ !!!!!

#6


-1  

I think you need to add the two escaped shortcuts into character classes. Try this: "[\d]{4}[\w]{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

我认为您需要将两个转义捷径添加到字符类中。试试这个:“\ d { 4 }[\ w]{ 3 }(0(1 - 9)|[12][0 - 9]| 3[1])([1][0 - 9]| 2(0 - 3))((0 - 5)[0 - 9]{ 2 }”

--Good Luck.

——祝你好运。

#1


49  

Assuming this regex is inside a Java String literal, you need to escape the backslashes for your \d and \w tags:

假设这个正则表达式位于Java字符串的字面意思中,那么您需要为您的\d和\w标记摆脱反斜杠:

"\\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

This gets more, well, bonkers frankly, when you want to match backslashes:

坦白地说,当你想要匹配反斜杠时,这种情况会变得更糟:

public static void main(String[] args) {        
    Pattern p = Pattern.compile("\\\\\\\\"); //ERM, YEP: 8 OF THEM
    String s = "\\\\";
    Matcher m = p.matcher(s);
    System.out.println(s);
    System.out.println(m.matches());
}

\\ //JUST TO MATCH TWO SLASHES :(
true

#2


6  

What about the following: \\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

关于以下:\ \ d { 3 } { 4 } \ \ w(0(1 - 9)|[12][0 - 9]| 3[01])([01][0 - 9]| 2(0 - 3))([0 - 9][0 - 5]){ 2 }

#3


1  

Have you tried this?

你有试过吗?

\\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

#4


0  

Did you try "\\d" and "\\w"?

你试过“\\”和“\\w”吗?

-edit- Lol I posted the right answer and get down voted and then I notice that * escapes backslashes so my answer appeared wrong :)

-编辑- Lol我发布了正确的答案,然后投票,然后我注意到*逃回了反斜杠,所以我的答案出现了错误:)

#5


0  

all you need to do is to put

你所要做的就是把它放进去。

 *\
 ex: string ex = 'this is the character: *\\s';

before your invalid character and not 8 \ !!!!!

在您的无效字符之前,而不是8 \ !!!!!

#6


-1  

I think you need to add the two escaped shortcuts into character classes. Try this: "[\d]{4}[\w]{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

我认为您需要将两个转义捷径添加到字符类中。试试这个:“\ d { 4 }[\ w]{ 3 }(0(1 - 9)|[12][0 - 9]| 3[1])([1][0 - 9]| 2(0 - 3))((0 - 5)[0 - 9]{ 2 }”

--Good Luck.

——祝你好运。