Java-Wildcard Character

Wildcard Character

A regular expression can be made up of ordinary characters, which are upper and lowercase letters and digits, plus sequences of metacharacters (A metacharacter is a character that has a special meaning to a computer program). The pattern in the previous program was just the word “java”, but what if you wanted to search a string for occurrences of “age” or “axe” or “ace” or even any three-letter word beginning with “a” and ending with “e” ?

You can deal with any of these possibilities with regular expressions. One option is to specify the middle character as a wildcard (a wildcard is a symbol used to replace or represent characters) by using a period; a period is one example of a meta-character. This meta-character matches any character except end-of-line, so the regular expression “a.e”, represents any sequence of three characters that starts with “a” and ends with “e”.

Program

 

Example 2


Program Source

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

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

Program Source

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

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

Leave a Comment