1 PackageTest_demo.zhengzebiaodashi;2 3 Importorg.junit.Test;4 ImportJava.util.regex.Matcher;5 ImportJava.util.regex.Pattern;6 7 Public classZhengzeclass {8 9 /*Ten * The Java.util.regex package consists of the following three classes: One * Pattern class: A * The Pattern object is a compiled representation of a regular expression. The Pattern class has no public constructor method. - * To create a pattern object, you must first call its public static compilation method, which returns a Pattern object. The method takes a regular expression as its first argument. - * Matcher class: the * The Matcher object is an engine that interprets and matches an input string. Like the pattern class, Matcher does not have a public construction method. - * You need to call the Matcher method of the Pattern object to get a Matcher object. - * Patternsyntaxexception: - * Patternsyntaxexception is a non-mandatory exception class that represents a syntax error in a regular expression pattern. + * Detailed View:http://www.runoob.com/java/java-regular-expressions.html - */ + A /** at * Determine if text contains a string - *-use regular expressions. *runoob.* is used to find if the Runoob substring is wrapped in a string - */ - @Test - Public voidtestmatches () { -String content = "I am Noob" + in"From Runoob.com."; -System.out.println ("Content:" +content); to +String pattern = ". *runoob.*"; -System.out.println ("Pattern:" +pattern); the * BooleanIsMatch =pattern.matches (Pattern, content); $System.out.println ("Does the string contain ' Runoob ' substrings?" +isMatch);Panax Notoginseng } - the /** + * Split String A * The-matcher.find () method is partially matched; the Matcher.matches () method is a full match the *-group (0) denotes the entire expression + */ - @Test $ Public voidTestmatchesgroup () { $ //find in string by specified pattern -String content = "This order is placed for qt3000! OK? "; -String pattern = "(\\d*) (\\d+) (. *)"; the //Create a Pattern object -Pattern r =Pattern.compile (Pattern);Wuyi //now create the Matcher object theMatcher m =r.matcher (content); - if(M.find ()) { WuSystem.out.println ("group (0) value:" + m.group (0)); -System.out.println ("group (1) Value:" + m.group (1)); AboutSystem.out.println ("group (2) Value:" + m.group (2)); $System.out.println ("group (3) Value:" + m.group (3)); -SYSTEM.OUT.PRINTLN ("Total number of groups::" +M.groupcount ()); -}Else { -System.out.println ("NO MATCH"); A } + } the - @Test $ Public voidTestmatcheshbaseput () { theString content = "Put ' t_test ', ' test-ugid ', ' f1:phone_no ', ' 15261851357 '"; theString Putpattern = "^put\\s*" (. *?) ' \\s*,\\s* ' (. *?) ' \\s*,\\s* ' F1: (. *?) ' \\s*,\\s* ' (. *?) ' $"; thePattern p =Pattern.compile (putpattern); theMatcher m =p.matcher (content); - //the Matches () method is a full match in if(M.matches ()) { theSystem.out.println ("group (0) value:" + m.group (0)); theSYSTEM.OUT.PRINTLN ("Total number of groups::" +M.groupcount ()); About for(inti = 1; I <= m.groupcount (); i++) { theSystem.out.println ("group (" + i + ") Value:" +M.group (i)); the } the } + } - the /**Bayi * Statistics matching times the * The-start method returns the initial index of the subsequence captured by a given group during the previous matching operation; the * The-end method adds 1 to the index of the last matching character. - */ - @Test the Public voidTestmatchnumber () { the //matches a word boundary, which is the position between the word and the space. theString REGEX = "\\bcat\\b"; theString INPUT = "Cat cat cat Cattie Cat"; - thePattern p =Pattern.compile (REGEX); theMatcher m = P.matcher (INPUT);//get Matcher Object the intCount = 0;94 the while(M.find ()) { thecount++; theSystem.out.println ("Match Number:" +count);98 //The Start method returns the initial index of the subsequence captured by the given group during the previous matching operation. AboutSystem.out.println ("Start ():" +M.start ()); - //The End method adds 1 to the index of the last matching character. 101System.out.println ("End ():" +m.end ());102 }103 }104 the /**106 * Text Substitution107 * The-replacefirst method is used to replace the text of the first matching regular expression;108 * The-replaceall method is used to replace all text that matches the regular expression. 109 */ the @Test111 Public voidTestreplaceall () { theString REGEX = "Dog";113String INPUT = "the dog says Meow." All dogs say meow. "; theString REPLACE = "Cat"; the thePattern p =Pattern.compile (REGEX);117Matcher m =P.matcher (INPUT);118 //INPUT = M.replacefirst (REPLACE);119String output =M.replaceall (REPLACE); - 121System.out.println ("Before replace:" +INPUT);122SYSTEM.OUT.PRINTLN ("After replace:" +output);123 }124 the}
The regular expression of Java