| Expression | Meaning |
| \s | Space Character |
| \S | Except Space any character |
| \d | Any Digit |
| \D | Except Digit any character |
| \w | Any word character [a-zA-Z0-9] |
| \W | Special Chracter i.e Except word character |
| . | Any character |
package demo.example3;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Tyson
*
* Search for chracters which already have special meaning in Java Regex
*/
public class Demo3 {
public static void main(String[] args) {
// add \ before\s for escape because \ is a escape character
printMatch("\\s", "a7b k@9", "Search for Space");
printMatch("\\S", "a7b k@9", "Search for anything Except Space");
printMatch("\\d", "a7b k@9", "Search for only digit");
printMatch("\\D", "a7b k@9", "Search for aything except digit");
printMatch("\\w", "a7b k@9", "Search for alphanumeric character");
printMatch("\\W", "a7b k@9", "Search for Special Character");
printMatch(".", "a7b k@9", "Search for Any Character");
}
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() + "..." + m.group());
}
System.out.println("\n");
}
}
Output :
