Character classes in Regular Expression

Expression Meaning
[abc] Either ‘a’ or ‘b’ or ‘c’
[^abc] Except ‘a’, or ‘b’ or ‘c’
[a-z] Any lower case alphabet symbol
[A-Z] Any upper case alphabet symbol
[a-zA-Z] Any alphabet symbol
[0-9] Any digit from 0-9
[a-zA-Z0-9] Any alphanumeric character
[^a-zA-Z0-9] Special Character i.e Except Alphanumeric characters
package demo.example2;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

 * @author Tyson
 *         Searching for a group of things together
 *         Character Classes Example
public class Demo2 {
	public static void main(String[] args) {
		printMatch("[abc]", "a7b@2#9", "Find a or b or c");
		printMatch("[^abc]", "a7b@2#9", "Find Excepet a or b or c");
		printMatch("[a-z]", "a7b@2#9", "Find anything between a to z");
		printMatch("[0-9]", "a7b@2#9", "Find any digit");
		printMatch("[a-zA-Z0-9]", "a7b@2#9", "Find any alphanumeric value");
		printMatch("[^a-zA-Z0-9]", "a7b@2#9", "Find any special characters ie excepet alphanumeric");

	public static void printMatch(String regularExpression, String targetString, String comment) {
		Pattern p = Pattern.compile(regularExpression);
		Matcher m = p.matcher(targetString);
		System.out.println("===" + regularExpression + "===in===" + targetString + "=== " + comment);
		while (m.find()) {
			System.out.println(m.start() + "..." +;

Leave a Comment