You can change yours like this and it will work:
public static void main(String[] args) throws FileNotFoundException {
System.out.println(isValidFirstChar("-test"));
System.out.println(isValidFirstChar("\\test"));
System.out.println(isValidFirstChar("\'test"));
System.out.println(isValidFirstChar("test"));
}
public static final Pattern VALID_FIRST_CHARACTERS = Pattern.compile("^[\\\\ \\' \\-].*");
public static boolean isValidFirstChar(String name) {
if (VALID_FIRST_CHARACTERS.matcher(name).matches()) {
return true;
}
return false;
}
The result of this is:
true
true
true
false
You must escape the \\
for java but also for regex that is why you need \\\\
this will become \\
when translated for the regex...
The .*
on the end means match anything after...So it starts with \ or ' or -
and is followed by anything.