Java-Predefined Character Classes

Predefined Character Classes

You also have a number of predefined character classes that provide you with a shorthand notation for commonly used sets of characters. Following table gives some that are particularly useful:

CHARACTER CLASS DESCRIPTION
\d This represents any digit and is therefore shorthand for [0-9].
\D This represents any character that is not a digit. It is therefore equivalent to [^0-9].
\s This represents any whitespace character.
\S This represents any non-whitespace character and is therefore equivalent to [^\s].
\w This represents a word character, which corresponds to an upper- or lowercase letter,
a digit, or an underscore. It is therefore equivalent to [a-zA-Z_0-9].
\W This represents any character that is not a word character, so it is equivalent to [^\w]

Note that when you are using any of the sequences that start with a backslash in a regular expression, you need to keep in mind that Java treats a backslash as the beginning of an escape sequence. Therefore, you must specify the backslash in the regular expression as \\. For example, to find a sequence of three digits, the regular expression would be “\\d\\d\\d”. This is peculiar to Java because of the significance of the backslash in Java strings.

Obviously, you may well want to include any of the other meta-characters, as part of the character sequence you are looking for. To do this you can use an escape sequence starting with a backslash in the expression to define such characters. Because Java strings interpret a backslash as the start of a Java escape sequence, the backslash itself has to be represented as \\, the same as when using the predefined character sets that begin with a backslash.

Program One

 

Program Two

 

Program Three



Program One Source

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

public class Javaapp {
  
    public static void main(String[] args) {
        
        Pattern pat = Pattern.compile("\\D{3}\\d{3}");
        Matcher mat = pat.matcher("ABC123Xabc456UDEF789");
        
        int i = 0;
        while(mat.find())
        { 
            i++;
            System.out.println(i+"th subsequence  : "+mat.group());
        }
    }
}

Program Two Source

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

public class Javaapp {
  
    public static void main(String[] args) {
        
        Pattern pat = Pattern.compile("\\S{3}\\s\\S{3}");
        Matcher mat = pat.matcher("AS3 67#6FF 6*?@4 ASD  345");
        
        int i = 0;
        while(mat.find())
        { 
            i++;
            System.out.println(i+"th subsequence  : "+mat.group());
        }
    }
}

Program Three Source

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

public class Javaapp {
  
    public static void main(String[] args) {
        
        Pattern pat = Pattern.compile("\\w\\w\\W\\W");
        Matcher mat = pat.matcher("Wz#$GH&^@#ReTg*&()");
        
        int i = 0;
        while(mat.find())
        { 
            i++;
            System.out.println(i+"th subsequence  : "+mat.group());
        }
    }
}

Leave a Comment