Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

I have a problem with a regex in java.

When I try to use this regex:

 ^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$  

I get the following error

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

I don't know how to handle that error. I already tried to double the backslashes, but it didn't work. I hope someone can help me with this.

Thanks

like image 734
Annika Avatar asked Apr 23 '13 20:04

Annika


People also ask

Which one is an invalid escape sequence?

Placing a '\' (backslash) in front of the character in the regular expression generates an 'Invalid escape sequence' compilation error. This only occurs when the regular expression is used in the text of the script.

Which is the invalid escape sequence in Javascript 1 point r b/e f?

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


1 Answers

This should work ^(?:(?:([01]?\\d|2[0-3]):)?([0-5]?\\d):)?([0-5]?\\d)$

The reason is that the listed symbols in the error message have special meaning, but \d is not one of those defined special symbols for using \, this means you have to escape it (by adding an extra \ in front of the symbol).

like image 70
Uku Loskit Avatar answered Oct 02 '22 02:10

Uku Loskit