Msgstr "Invalid escape sequence (valid - \ b \ t \ n \ f \ r \" \ '\\) "Syntax error

I wrote code to map the path to a file that has extenstion.ncx,

 pattern = Pattern.compile("$(\\|\/)[a-zA-Z0-9_]/.ncx");
 Matcher matcher = pattern.mather("\sample.ncx");

This shows an invalid escape sequence (valid: \ b \ t \ n \ f \ r \ "\ '\) a syntax error pattern. How can this be fixed.

+3
source share
3 answers
Pattern p = Pattern.compile("[/\\\\]([a-zA-Z0-9_]+\\.ncx)$");
Matcher m = p.matcher("\\sample.ncx");
if (m.find())
{
  System.out.printf("The filename is '%s'%n", m.group(1));
}

output:

The filename is 'sample.ncx'

$ anchors match to the end of the line (or to the end of the line in multi-line mode). It is at the end of your regular expression, not at the beginning.

[/\\\\] , . , , . .

[a-zA-Z0-9_]+ ; plus, .

, , dot --and , Java.

alternation (|) , . , , .

+5

java \ . \.

pattern=Pattern.compile("$(\\\\|\\/)[a-zA-Z0-9_]/.ncx");
+5

try it

$(\\|\\/)[a-zA-Z0-9_]/.ncx
+2
source

Source: https://habr.com/ru/post/1791402/


All Articles