I'm trying to use something like
public static final Pattern p1 = Pattern.compile("\[(.*?)\]"); public static final Pattern p1 = Pattern.compile("\[(\w)\]");
what I get:
Invalid escape sequence (valid ones are \ b \ t \ n \ f \ r \ "\ '\)
Why?
I'm trying to use something like
public static final Pattern p1 = Pattern.compile("\[(.*?)\]"); public static final Pattern p1 = Pattern.compile("\[(\w)\]");
what I get:
Invalid escape sequence (valid ones are \ b \ t \ n \ f \ r \ "\ '\)
Why?
The \
character is used in java for escape sequences (the very \b \t \n \f \r \" \'
that the compiler speaks of. Therefore, to use it in the string, you need to escape:
public static final Pattern p1 = Pattern.compile("\\[(.*?)\\]"); public static final Pattern p1 = Pattern.compile("\\[(\\w)\\]");
Pattern.compile("\\\"(.?)\\\"");
quite adequate option - Nofate ♦Duplicate the slash: " \\[(.*?)\\]
"
Source: https://ru.stackoverflow.com/questions/101750/
All Articles