Using Wildcards and Quantifiers
The following example that uses the + quantifier to match any arbitrarily long sequence of Ws.
// Use a quantifier.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
Pattern pat = Pattern.compile("A+");
Matcher mat = pat.matcher("A AA AAA");
while (mat.find()){
System.out.println("Match: " + mat.group());
}
}
}
The next program uses a wildcard to create a pattern that will match any sequence that begins with e and ends with d.
// Use wildcard and quantifier.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
Pattern pat = Pattern.compile("e.+d");
Matcher mat = pat.matcher("extend electronics ebook-iPad ");
while (mat.find())
System.out.println("Match: " + mat.group());
}
}
//Match: extend electronics ebook-iPad
Home
Java Book
Essential Classes
Java Book
Essential Classes
Matcher:
- Regular Expression Processing
- Normal character
- Wildcard character
- Using Wildcards and Quantifiers
- Greedy behavior
- Working with Classes of Characters
- Using replaceAll( )
- Using split( )
- Matcher: appendReplacement(StringBuffer sb,String replacement)
- Matcher.appendTail(StringBuffer sb)
- Matcher: find()
- Matcher: group()
- Matcher: group(int group)
- Matcher: groupCount()
- Matcher: lookingAt()
- Matcher: matches()
- Matcher: replaceAll(String text)
- Matcher: start()