Java regular expression match, replace, find, cut

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.