An example of the application of a Java regularization expression (illustrated by the matches method of string class) __ Regular expressions

Source: Internet
Author: User

Examples of the application of Java's regular expressions (see Java's application of the regular Expressions): Test code   package test;  /**    *  In string matches () method, the split () method uses regular expressions .    *  @author  fhd001    */    public class regextest {          public  Static void main (String[] args)  {               /*            *  Ordinary characters             */            String str1 =  "abc45abc345";            String[]arr1 = str1.split ("abc");           for   (STRING STRING : ARR1)  {                system.out.print (string+ "-->");           }            SYSTEM.OUT.PRINTLN ();                                   /*             *  Simple escape characters             *  You must use a double backslash when you are using Java to relay the literal characters. Two slash escape into a slash,            *  escape those special characters with this slash character .             */            String str2 =  ^$ () []{}.? +*|";            boolean flag = str2.matches ("//^//$ //(//)//[//]//{//}//.//?//+//*//|");            SYSTEM.OUT.PRINTLN (flag);                                   /*            *  Escape Character  /q.../e .            */            String str3 =  ^$ () []{}.? +*|99999 ";           boolean flag2 =  Str3.matches ("//q^$ () []{}.? +*|//E//D{5} ");           system.out.println (FLAG2);                                   /*             *  Character Set             */            String str4 =  "dfddri334";            boolean flag4 = str4.matches (". +");            system.out.println (FLAG4);                      String str5 =  "#$%^* ())%";            boolean flag5 = str5.matches ("//W{9}");           system.out.println (FLAG5);                       string str6 =   "4GFFMDKEKRHHR";           boolean flag6 =  str6.matcheS ("//w+");           system.out.println (FLAG6);                       string str7 =  "fjfdke    eett ";            boolean flag7 = str7.matches ("//w+//s+//w{4}//s?");            system.out.println (Flag7);                       String  str8 =  "   erefff    ";            boolean flag8 = str8.matches ("//s+//s+//s+");            system.out.println (FLAG8);                       string str9 =  "456776888";            boolean flag9 = str9.matches ("//d+");            system.out.println (FLAG9);                                   String str10 =  "rtydfgrgwvr  ";            boolean flag10 = str10.matches ("//D+");            system.out.println (FLAG10);                                   /*            *  custom character set  [ ]            */            String str11 =  "fdfeetg  34566";            boolean flag11 = str11.matches ("[fdetg]+//s+[3-6]+");           system.out.println (flag11);                                   String str12 =  "rtyuie   5768";            boolean flag12 = str12.matches ("[^abcdf]+ s+[^1234]+ ");           system.out.println (FLAG12);                         &nbsP         /*             *  matching number qualifier             */                       //Greedy mode            String str13 =  " ytreggcv454444444333 ";           boolean flag13 =  str13.matches ("//w{20}");           system.out.println ( FLAG13);                       boolean flag14 = str13.matches ("//w{10,21}");            system.out.println (FLAG14);                        boolean flag15 = str13.matches ("//w{18,}");            SYSTEM.OUT.PRINTLN (FLAG15);                       String  str14 =  "4";           boolean flag16 =  str14.matches ("//d?");            system.out.println (FLAG16);                       string  str15 =  "DDCVGT";           boolean  Flag17 = str15.matches ("//d+//d?");            system.out.println (flag17);                       string str16 =  "e33tf44t44t";            boolean flag18 = str16.matches ("//w+//W*");           system.out.println (flag18);                       //(just one example)             String str17 =  "34567ghjkkld";            boolean flag19 = str17.matches ("//d{2,7}"? w{8,11} ");           system.out.println (flag19);                        //occupancy mode (one case only)            String str18 =  " 22222DDDDD ";   &NBSp;       boolean flag20 = str18.matches ("//d{2,5}+//w{6}");            system.out.println (FLAG20);                       /*             *  character Boundaries              */           string str19  =  "A444545rot44tm";           boolean flag21  = str19.matches ("^a//w+m$");            System.out.println (flag21);                                  /*             *  Selection Expressions  xxx | xxx             */           STRING STR20  =  "ABC123ABC";           boolean flag22  = str20.matches ("(abc|123) {3}");            System.out.println (flag22);                                  /*             *  Group   ( ) .             *  above modifiers are decorated for one character. If you want to make a set of characters              *  decoration to be used ()              */   &Nbsp;       string str21 =  "123qwe123qwe";            boolean flag23 = str21.matches ("(123qwe) {2}");            SYSTEM.OUT.PRINTLN (flag23);                                  /*             * [] in the intersection and the set             */            String str22 =  "abcdefgh1234567";            boolean flag24 = str22.matches ("[a-z1-9]+");         //Collection             System.out.println (fLAG24);                       boolean flag25 = str22.matches ("[a-z1-9&&[a-h1-7]]+");  //intersection            system.out.println (flag25);        }  }  

  Result code  -->45-->345-->   true   true   true   True    true   true   true   true   true   true   true   true   true   true   true   true     true   false   true   true   true   true   True  

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.