Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
I have this code:
Java
Pattern.compile("^[SFTG]\d{7}[A-Z]$");

And I get this error:
Invalid escape sequence in ^[SFTG]\d{7}[A-Z]$

How can I solve this error?
Posted
Updated 12-Dec-14 4:13am
v2

 
Share this answer
 
The string containing you regular expression has a backslash in it:

Pattern .compile("^[SFTG]\d{7}[A-Z]$");


It should look like this instead:

Pattern .compile("^[SFTG]\\d{7}[A-Z]$");
//                       ^^-------------- Double backslash needed here because backslash is an escape character in strings.



See also here: https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6[^]

Regards,

Manfred
 
Share this answer
 
Comments
Maciej Los 17-Nov-14 13:41pm    
And what does your answer differs from mine, Manfred?
Manfred Rudolf Bihy 17-Nov-14 15:22pm    
I didn't read your solution, sorry!
Maciej Los 17-Nov-14 16:00pm    
Not to worry ;)
+5! for direct instructions...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900