Preliminary study on regular expression of java--

Source: Internet
Author: User

The regular expression is a kind of character processing tool, which can find, extract, divide and replace the string. A regular expression can be understood as a template to match a string. Using regular Expressions in Java, we pre-define a rule and see if the string conforms to the rule. Of course we need some special symbols when defining rules, which are special characters in regular expressions.
There are two ways to use the regular in Java, 1 is called directly through the string class, and 2 is used through the pattern and Matcher classes in Java.
See the code and comments below for more detailed usage.

The code is as follows:

Package Lkl1;import Java.util.regex.matcher;import Java.util.regex.Pattern; Public classRegular Expression { Public Static void Main(string[] args) {//Call regular expressions directly through stringsString str ="Test Java";when a regular expression substitution is called directly from a string, it does not change the value of the original string, but          //There are other return valuesString str1 = Str.replacefirst ("\\w*","haha"); System. out. println (STR1); System. out. println (Str.replaceall ("\\w+"," very well"));//Determines whether the string matches the specified regular expression (whole string match)          if(Str.matches ("\\w")) {System. out. println ("The letters in the string are word characters"); }Else{System. out. println ("The string contains non-word characters"); }//Specifies the delimiter to decompose the string into multiple substringsString s[] = Str.split (" "); for(String ss:s) {System. out. println (ss); }//Use regular expressions with the pattern and Matcher classes in JavaString st ="Java is very easy"; Pattern p = pattern.compile ("\\w+");//Specify regular expression patternMatcher m = P.matcher (ST);//Specify a string that uses regular expressions          //find (); Returns whether there are substrings in the string that match pattern          //In a string, find () is the individual substring of the previous lookup          //Can pass in the parameter I, indicating that the match starts from the first I positionBoolean bl = M.find (); System. out. println (BL);//Returns the last substring matching the pattern          //start (); Returns the starting position of the last string matched with the pattern          //end (); Returns the end position of the last string matched with the pattern           while(M.find ()) {System. out. Print (M.Group()+"\ T"); System. out. println ("The starting position of the substring is:"+m.start () +"The terminating position is:"+m.end ()); }//lookingat () returns whether the preceding part of the target string matches the patternSystem. out. println ("Does the preceding part of the string match the pattern:"+m.lookingat ());//Whether the entire string matches the patternSystem. out. println ("Does the entire string match the pattern:"+m.matches ());//reset (), using the existing matcher for a new stringM.reset ("LKL");//Integrated use: Check the mailbox format is correctstring[] mails = {"[email protected]","[email protected]","[email protected]","[email protected]"}; Pattern P1 = Pattern.compile ("\\w{3,20}@\\w+\\. (com|gov|org) ");//actually can also direct: "[email protected]". Matcher ("\\w{3,20}@\\w+\\." ( com|gov|org) ")Matcher M1 =NULL; for(String mail:mails) {if(m1==NULL{m1 = p1.matcher (mail); }Else{m1 = m1.reset (mail); }if(!m1.matches ()) {System. out. println ("Mailbox:"+mail+"Name not valid"); }           }         }}

Preliminary study on regular expression of java--

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.