Importjava.util.ArrayList;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classTest { Public voidMain () {getstrings ();//gets the specified content in the specified string content with a regular expressionSystem.out.println ("********************"); Replace (); //Replace string contents with regular ExpressionsSystem.out.println ("********************"); Strsplit (); //using regular expressions to cut stringsSystem.out.println ("********************"); Strmatch (); //string Match } Private Static voidStrmatch () {String phone= "13539770000"; //check whether the phone is a qualified mobile number (standard: 1, second is 3,5,8, and the next 9 digits are any number)SYSTEM.OUT.PRINTLN (Phone + ":" + phone.matches ("1[358][0-9]{9,9}"));//trueString str= "ABCD12345EFGHIJKLMN"; //Check if the middle of STR contains 12345SYSTEM.OUT.PRINTLN (str + ":" + str.matches ("\\w+12345\\w+"));//trueSYSTEM.OUT.PRINTLN (str + ":" + str.matches ("\\w+123456\\w+"));//false } Private Static voidStrsplit () {String str= "ASFASF.SDFSAF.SDFSDFAS.ASDFASFDASFD.WRQWRWQER.ASFSAFASF.SAFGFDGDSG"; String[] STRs= Str.split ("\ \.")); for(String s:strs) {System.out.println (s); } } Private Static voidgetstrings () {String str= "Rrwerqq84461376qqasfdasdfrrwerqq84461377qqasfdasdaa654645aafrrwerqq84461378qqasfdaa654646aaasdfrrwerqq84461379qqasfdas Dfrrwerqq84461376qqasfdasdf "; Pattern P= Pattern.compile ("QQ (. *?) Qq); Matcher m=P.matcher (str); ArrayList<String> STRs =NewArraylist<string>(); while(M.find ()) {Strs.add (M.group (1)); } for(String s:strs) {System.out.println (s); } } Private Static voidreplace () {String str= "ASFAS5FSAF5S4FS6AF.SDAF.ASF.WQRE.QWR.FDSF.ASF.ASF.ASF"; //replace the. In the string with _, because. is a special character, so use \. expression, and because \ is a special character, so use \ \ to express.str = str.replaceall ("\ \", "_"); System.out.println (str); }}
Java regular Expressions match, replace, find, cut (GO)