Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use Regex in Java because of escape sequence error, how to remove the error

I have this regex :

^(([A-Z]:)|((\\|/){1,2}\w+)\$?)((\\|/)(\w[\w ]*.*))+\.([txt|exe]+)$

but every time I assign it to any string, Eclipse returns me invalid escape sequences, I have inserted a backward slash but it gives me the same error.

How to assign the above expression to string in java?

like image 806
M. A. Avatar asked Dec 06 '25 17:12

M. A.


1 Answers

Replace all "\\" with "\\\\". Java has no language support for regular expressions. So you'll need "\\" to get a backslash from the Compiler into the String. If the regular expression shall contain an escaped backslash, you need "\\\\".

final String re = "^(([A-Z]:)|((\\\\|/){1,2}\\w+)\\$?)((\\\\|/)(\\w[\\w ]*.*))+\\.([txt|exe]+)$"
like image 106
Christian Hujer Avatar answered Dec 08 '25 08:12

Christian Hujer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!