Pattern p = pattern.compile ("a*b"= P.matcher ("Aaaab"); Boolean // B = True
Pattern objects can be reused multiple times. If a regular expression needs to be used only once, you can use the static matches method of the pattern class directly:
Boolean b = pattern.matches ("A*b", "Aaaab");
Pattern is an immutable class that can be used safely by multiple concurrent threads.
Matcher class Examples:
ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classFindgroupdemo { Public Static voidMain (string[] args) {Matcher m= Pattern.compile ("\\w+"). Matcher ("Java is very easy"); while(M.find ()) {System.out.println (M.group ()); } inti = 0; while(M.find (i)) {System.out.print (M.group ()+ "\ T"); I++; } }}
The Find () method finds substrings in a string that match the pattern one time, and once the corresponding substring is found, the next time the Find () method is called, it is then looked down. In addition, find () can also wear parameters of type int, and the Find () method searches down from the int index.
ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classStartenddemo { Public Static voidMain (string[] args) {String regstr= "Java is very easy!"; System.out.println ("Target string:" +regstr); Matcher m= Pattern.compile ("\\w+"). Matcher (REGSTR); while(M.find ()) {System.out.println (M.group () )+ "Start:" + m.start () + ", End:" +m.end ()); } }}
The start and end methods are primarily used to determine the position of a substring in a target string
"Crazy Java Handout" (22)----Regular Expressions