Java Learning notes-Regular expressions

Source: Internet
Author: User

NO Method name Type Describe
1 Public boolean matches (String regex) Ordinary Regular validation uses the
2

public string ReplaceAll (string regex,string replacement)

Ordinary Replace All
3

public string Replacefirst (string regex,string replacement)

Ordinary Replace the first
4 Public string[] Split (String regex) Ordinary Split all
5

Public string[] Split (String regex,int limit)

Ordinary Partial split

Regular symbols (all regular match symbols are provided in Java.util.regex.Pattern;):

1. Represents a single character (each occurrence of one represents a bit):

    • X: The representation is made up of X;
    • \ \: Represents the transfer character "\"
    • \ t: Indicates a tab, \ n = line break

2. Indicates the selection of characters (one for each occurrence):

  • [ABC]: Indicates that it may be any of a B C (note: Only one character can be matched);
  •  1  public  class   Demo1 { 2  3  public  static  void   Main (string[] args) { 4  //  TODO auto-generated method stub  5  String str= "a" ;  6  System.out.println (str.matches ("[ABC]" )); /span>7   " 8  9 } 
    View Code
  •  Public class demo1 {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        String str= "AB";        System.out.println (Str.matches ("[ABC][ABC]");}    }
    View Code
  • [^ABC]: denotes any character except A, B, or C (negation)

  • [A-za-z]:a to Z or A to Z, the letters at both ends are included (range)

  • [0-9]:

3. Simplify the expression (one for each occurrence):

  • .: denotes any one character;
  • \d: Represents any one digit, equivalent to [0--9];
  •      Public Static void Main (string[] args) {        //  TODO auto-generated method stub        String str= "2";        System.out.println (Str.matches ("\\d"));}    }
    View Code
  • \d: Indicates not a number, equivalent to [^0--9];
  • \s: denotes any one space;
  •  Public class demo1 {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        String str= "\ t";        System.out.println (Str.matches ("\\s"));}    }
    View Code
  • \s: Represents a space that is not arbitrary;
  • \w: denotes any digit in the alphanumeric underline of letters (including uppercase or lowercase);

4. Boundary Matching:

    • ^: written before the regular, indicating the beginning of the regular;
    • $: Written at the end of the regular, indicating a regular end;

5. Digital Specifications:

    • ?: Indicates that the regular specification can only occur 0 or 1 times;
    •  public  class   Demo1 { static  void   main (string[] args) {//  TODO auto-generated method stub  Strin        G str= "" ; System.out.println (Str.matches ( "\\d?")         = "
            
             ; System.out.println (Str1.matches (
             "\\d?")     
      view code
    • *: Indicates that there are 0 times 1 or more times;
    • +: Indicates regular occurrences of 1 times & nbsp, or more than once;
    • {n}: Indicates that this regular occurs exactly n times;
    • {n}: Indicates that the regular appears n times and more than n times;
    • {n,m}: Indicates that this regular appears n~m;

6. Logical expression:

    • Regular x regular Y: Executes the regular x after the regular y;
    • X| Y: One of the regular x or regular y can be satisfied;
    • (regular): Indicates that more than one regular becomes a group;

7. Manipulating characters by using the String class:

  • such as: Keep only letters
  • 1  Public classDemo1 {2      Public Static voidMain (string[] args) {3         //TODO auto-generated Method Stub4String str= "AFAJHJ2313535{}]GAJ>? '; F ' Afag;lag ";5String regex= "[^a-za-z]";6System.out.println (Str.replaceall (Regex, ""));7     }8}
    View Code
  • Replace (with the split () function)
  •  Public class demo1 {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        String str= "a1b22c333d4444e55555f666666g";        String regex= "\\d+";        String [] Res=str.split (regex);          for (int i=0;i<res.length;i++)            System.out.println (Res[i]);}    }
    View Code
  • The user name is represented by an alphanumeric underscore and has 6~15 characters;
  •  public  class   Demo1 { public  static  void   main (string[] args) { // 
         
           TODO auto-generated Method stub  String str= "wangxiang_123*" 
          ;        String regex  = "\\w{6,15}" 
          ;        System.out.println (Str.matches (regex)); String regex1  = "(\\d|[        a-za-z]|_|\\*) {6,15} "
          ;    System.out.println (Str.matches (REGEX1)); }}  
         
    view code
  • Now the students in a school record the "name: Age: Birthday: Results" format, such data can appear multiple times and separated by |, such as: allen:19:1993-05-02:98.6| judy:21:2000-09-02:78| sara:20:1994-08-21:97.5
  • The following is a split of the individual judgments:
    • Name:
    • String str= "Zhansan"; String regex= "[a-za-z]+";

    • Age:
    • String str= "n"; String regex= "\\d{1,3}";

    • Birthday:
    • String str= "1993-09-02"; String regex= "\\d{4}-\\d{2}-\\d{2}";

    • Results:
    • String str= "98.98"; String regex= "\\d{1,3} (\\.\\d{1,2})?";

    • Single Student format synthesis:
    • String str= "allen:19:1993-05-02:98.6"; String regex= "[a-za-z]+:\\d{1,3}:\\d{4}-\\d{2}-\\d{2}:\\d{1,3} (\\.\\d{1,2})?";

    • The whole judgment:
    •  Public class demo1 {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        String str= "allen:19:1993-05-02:98.6| judy:21:2000-09-02:78| sara:20:1994-08-21:97.5 ";        String Regex= "([a-za-z]+:\\d{1,3}:\\d{4}-\\d{2}-\\d{2}:\\d{1,3} (\\.\\d{1,2})?" \\|?) +";        System.out.println (Str.matches (regex));}    }

Java Learning notes-Regular expressions

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.