Pattern p = Pattern.compile("([a-z,A-Z,0-9,_, ,/,\\\,-,.])+[.]+ jar|class|txt)",Pattern.CASE_INSENSITIVE);
In Java regular expressions, when you have to use DOUBLE BACKSLASH as escape character. Java eats up one BACKSLASH, and the regular expression engine eats the other BACKSLASH.
Ajay A Patil
Greenhorn
Joined: Apr 13, 2006
Posts: 22
posted
0
Actually, you will also have to escape the '.' character.
Pattern p = Pattern.compile("([a-z,A-Z,0-9,_, ,/,\\\,-,.])+[\\.]+ jar|class|txt)",Pattern.CASE_INSENSITIVE);
Alan Moore
Ranch Hand
Joined: May 06, 2004
Posts: 262
posted
0
In fact, you need four backslashes in the regex string to match one in the target string. And as for the dot, if you put it in a character class, it just matches a dot. So you can either put brackets around it, or put a backslash in front of it, but doing both is overkill (although it still works). The comma also has no special meaning, Pradeep; your character class will match a comma as well as all those other characters. And you don't need the parentheses around the character class, but you do need a matched pair of them around the final alternation (I'm sure that's just a typo). (\w is the same as [A-Za-z0-9_])