Possible Duplicate:
JAVA: check a string if there is a special character in it
I'm trying to create a method to check if a password starts or ends with a special character. There were a few other checks that I have managed to code, but this seems a bit more complicated.
I think I need to use regex to do this efficiently. I have already created a method that checks if there are any special characters, but I can't figure out how modify it.
Pattern p = Pattern.compile("\\p{Punct}");
Matcher m = p.matcher(password);
boolean a = m.find();
if (!a)
System.out.println("Password must contain at least one special character!");
According to the book I'm reading I need to use ^ and $ in the pattern to check if it starts or ends with a special character. Can I just add both statements to the existing pattern or how should I start solving this?
EDIT:
Alright, I think I got the non-regex method working:
for (int i = 0; i < password.length(); i++) {
if (SPECIAL_CHARACTERS.indexOf(password.charAt(i)) > 0)
specialCharSum++;
}