Regular Expressions in Java:
1: Common presentation rules:
1 \ \ backslash character (ie: \)2 3 4 character class5 [ABC] A, B or C (simple Class)6[^ABC] Any character except A, B, or C (negation)7[a-za-Z] A to Z or A to Z, the letters at both ends are included (range)8[A-d[m-p]] A to D or M to p:[a-dm-p] (and set)9[a-z&&[def]] D, E or F (intersection)Ten[A-Z&&[^BC]] A to Z, except for B and c:[ad-z] (minus) One[A-z&&[^m-p]] A to Z, not m to p:[a-lq-z] (minus) A - Predefined character Classes - . Any character (may or may not match the line terminator)
\ transpose character (\ \ =: \,\\. means: \. ie: Point itself) the\d Number: [0-9] -\d non-numeric: [^0-9] - \s whitespace characters: [\t\n\x0b\f\r] -\s non-whitespace characters: [^\s] +\w Word character: [a-za-z_0-9] -\w non-word characters: [^\w] + A Boundary Matching Device at^the beginning of the line - end of the $ line - \b Word Boundaries - \b Non-word boundaries - \a The beginning of the input - \g The end of the previous match in \z the end of the input, only for the last terminator (if any) - \z end of input to + greedy number of words -X?X, not once or once thex*X, 0 or more times *x+X, one or more times $ X{n} X, exactly n timesPanax Notoginseng X{n,} X, at least n times -X{n,m} X, at least n times, but no more than m times
Example: 1: Determine whether the mobile phone number starts with 13 or 18 and satisfies 11 bits:
1 Public classRegexdemo {2 Public Static voidMain (string[] args) {3 //Keyboard Entry mobile phone number4Scanner sc =NewScanner (system.in);5System.out.println ("Please enter your mobile phone number:");6String phone =sc.nextline ();7 8 //rules for defining mobile numbers9String regex = "1[38]\\d{9}";Ten One //call function, Judge can A BooleanFlag =phone.matches (regex); - - //Output Results theSystem.out.println ("flag:" +flag); - } -}
2: Verify mailbox:
1 /*2 * Check Mailbox3 * 4 * Analysis:5 * A: Keyboard Entry Mailbox6 * B: Define the rules for the mailbox7 * [email protected]8 * [email protected]9 * [email protected]Ten * [email protected] One * [email protected] A * C: Call function, Judge can - * D: Output result - */ the Public classRegextest { - Public Static voidMain (string[] args) { - //Keyboard Entry Mailbox -Scanner sc =NewScanner (system.in); +System.out.println ("Please enter your email address:"); -String email =sc.nextline (); + A //define the rules for a mailbox at //String regex = "[A-za-z_0-9][email protected][a-za-z_0-9]{2,6} (\\.[ a-za-z_0-9]{2,3}) + "; -String regex = "\\[email protected]\\w{2,6} (\\.\\w{2,3}) +"; - - //call function, Judge can - BooleanFlag =email.matches (regex); - in //Output Results -System.out.println ("flag:" +flag); to } +}
Regular Expressions in Java