Basic collation of Java regular expressions

Source: Internet
Author: User
Tags valid email address

(a) Introduction to regular expressions and grammars

    1. The string class uses several methods of regular expressions:
    2. Valid characters supported by regular expressions:
    3. Special characters:
    4. Pre-defined characters:
    5. Square brackets Expression:
    6. Parenthesis expression: Used to make multiple expressions into a subexpression, you can use the OR operator "|", such as a regular expression: "(AA|BB|CC)" is to match "AA", "BB", "CC" One of the three strings.
    7. Boundary match:
    8. Greedy, reluctant, possessive number identifiers:

(ii) Simple usage of Java regular expressions

  1. Two key classes: (1) Pattern: The representation of a regular expression after it is compiled in memory. is an immutable class that can be used concurrently by multiple threads, (2) Matcher: Saves the various states involved in performing a match, and multiple Matcher objects can share a pattern object.
  2. Example of a simple usage program:
    1 // Output: True 2 Pattern p = pattern.compile ("a*b"); 3 Matcher m = P.matcher ("AABZAAADAAAFBC"); 4 // Output: False

  3. Common methods of the Matcher class:

  4. Examples of programs:
    1      Public Static voidtest1 () {2System.out.println (Pattern.matches ("A\\WB", "A_b"));//Output: True3 4Pattern p = pattern.compile ("A*b");5Matcher m = P.matcher ("AABZAAADAAAFBC");6 7System.out.println (M.matches ());//Output: False8System.out.println (M.find ());//Output: True9System.out.println (M.group ());//output: bTenSystem.out.println (M.start ());//Output: 2 OneSystem.out.println (M.end ());//Output: 3 ASystem.out.println (M.lookingat ());//Output: True -M.reset ("Zab"); -System.out.println (M.lookingat ());//Output: False the     } -  -      Public Static voidtest2 () { -Matcher m = pattern.compile ("\\w+"). Matcher ("Java is very easy!"); +  -          while(M.find ()) { +System.out.println (M.group () + "substring start position:" + m.start () + ", End position:" A+m.end ()); at         } -  -         inti = 0; -          while(M.find (i)) { -System.out.print (M.group () + "\ T"); -i++; in         } -  to         //Output: +         //starting position of the Java substring: 0, End position: 4 -         //is substring start position: 5, End position: 7 the         //very the starting position of the substring: 8, end Position: *         //Easy substring start position: 13, End Position: $         //Java Ava va A is are s very very ery ry y easy easy asy sy yPanax Notoginseng     } -  the      Public Static voidtest3 () { +string[] mails = {"[email protected]", "[email protected]", A"[Email protected]", "[email protected]" }; theString Mailregex = "\\w{3,20}@\\w+\\. (com|cn|edu|org|net|gov) "; +Pattern Mailpattern =Pattern.compile (Mailregex); -  $Matcher Mailmatcher =NULL; $  -          for(String mail:mails) { -             if(Mailmatcher = =NULL) { theMailmatcher =mailpattern.matcher (mail); -}Else {Wuyi mailmatcher.reset (mail); the             } -  WuSystem.out.println (Mail + (mailmatcher.matches)? "Yes": "No") -+ "A valid e-mail address"); About         } $  -         //Output: -         //[email protected] is a legitimate e-mail address -         //[email protected] is a legitimate e-mail address A         //[email protected] is not a valid email address +         //[email protected] is not a valid email address the  -     } $  the      Public Static voidtest4 () { theMatcher m = pattern.compile ("\\bre\\w*"). Matcher ( the"Java is real good at Inrestart and regex."); theSystem.out.println (M.replaceall ("haha")); -  in         //Output: the         //Java is haha good at inrestart and haha. the  About}

Finish

Basic collation of Java regular expressions

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.