I have a userid field that I only want to allow "a-zA-Z0-9". We are using jdsk 1.3.1, not 1.4, so we can't use import java.util.regex. How can I accomplish this?
Ok, I am able now to use jave.util.regex for this. The below code runs, but it asways comes back as "no match", meaning it didn't evaluate the string properly, because it should have returned true. Could someone help please.
Code:
public static void main(String[] args) {
String test = "ef(123"; //this should fail because of the (, but it always passes, and prints out no match below
Pattern p = Pattern.compile("\\w", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(test);
if(m.matches()==true){
System.out.println("match true");
} else {
System.out.println("no match");
}
}
Well all regex implementations are not created equal but I'm not sure whether this is absolutely wrong. It certainly doesn't match the same way the Javascript or VisualREGEXP regex do.
Having given the documentation a quick read, it seems that the matches(String regex, String input) method returns true if and only if the entire string matches the pattern. In otherwords, for the regex in your example to return true, every character would have to be a non-word character. Since that's not true in either case, bad is always set to false. The find() method of the matcher returns true if there is an occurrence of the pattern in the string:
Originally posted by HaganeNoKokoro Having given the documentation a quick read, it seems that the matches(String regex, String input) method returns true if and only if the entire string matches the pattern. In otherwords, for the regex in your example to return true, every character would have to be a non-word character. Since that's not true in either case, bad is always set to false. The find() method of the matcher returns true if there is an occurrence of the pattern in the string:
Hence the reason for the pattern I used in my reply above. Still this is a good illustration that there is not really a "standard" for regex matchers even though the patterns look very very similar.
Bookmarks