tags:

views:

78

answers:

1

I been trying to figure out how this blasted regex for two hours!!! It's midnight I gotta figure this out and go to bed!!!

String str = new String("filename\\");
if(str.matches(".*[?/<>|*:\"{\\}].*")) {
    System.out.println("match");
}else {
    System.out.println("no match");
}

".*[?/<>|*:\"{\\}].*" is my regex expression. It catches everything correctly except the backslash!!! I need to know how to make it catch the backslash correctly please help!

FYI, the illegal characters i'm trying to catch are ? \ / < > | * : " I've got it working exception for the backslash

+3  A: 

The problem is that \\ escapes a backslash in a Java String and you have to escape it in the regex. That means using four backslashes:

if ("ab\\d".matches("[abd\\\\]*") {
  // match
}

Because two of the backslashes are Java String escapes the regex is really:

[abc\\]*

and \\ is required in the regex to escape the backslash.

cletus
THANK YOU!!!!!!! For reason when I try to accept your answer as the correct answer it says I must wait 9 minutes :\ Thank you again now I can go bed happy!
cchampion