I'm trying to use the Java Pattern and Matcher to apply input checks. I have it working in a really basic format which I am happy with so far. It applies a REGEX to an argument and then loops through the matching characters.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexUtil {
public static void main(String[] args) {
String argument;
Pattern pattern;
Matcher matcher;
argument = "#a1^b2";
pattern = Pattern.compile("[a-zA-Z]|[0-9]|\\s");
matcher = pattern.matcher(argument);
// find all matching characters
while(matcher.find()) {
System.out.println(matcher.group());
}
}
}
This is fine for extracting all the good characters, I get the output
a
1
b
2
Now I wanted to know if it's possible to do the same for any characters that don't match the REGEX so I get the output
#
^
Or better yet loop through it and get TRUE or FALSE flags for each index of the argument
false
true
true
false
true
true
I only know how to loop through with matcher.find(), any help would be greatly appreciated
[^a-zA-Z0-9]will give you all characters not matching the range so: # ^