Java learns from scratch (Regular Expression), java starts from scratch

Source: Internet
Author: User

Java learns from scratch (Regular Expression), java starts from scratch
I. Why do we need regular expressions to easily match data and perform more complex string verification, splitting, and replacement functions. For example, to determine whether a string is composed of digits, you can use either of the following methods: if you want to apply a regular expression in a program, you must rely on the Pattern class and Matcher class. Both classes are in java. util. defined in the regex package. The main function of the Pattern class is to compile the regular expression specification, while the Matcher class mainly implements the specification to verify whether a string conforms to its specification.Common regular rules

No. Specifications Description No. Specifications Description
1 \\ Backslash (\) character 2 \ T Tab
3 \ N Line feed 4 [Abc] Character a, B, or c
5 [^ Abc] Any character except a, B, and c 6 A-zA-Z0-9 It is composed of letters and numbers.
7 \ D Number 8 \ D Non-numeric
9 \ W Letters, numbers, and underscores 10 \ W Non-letters, numbers, and underscores
11 \ S All blank characters (line breaks, spaces, etc) 12 \ S All non-blank characters
13 ^ Start of a row 14 $ End of a row
15 . Match any character except line breaks

 

 

 

  Number representation (X indicates a set of specifications)
 No. Specifications Description No. Specifications Description
1 X Must appear once 2 X? It can appear 0 or 1 time
3 X * It can appear 0, 1, or multiple times 4 X + Can appear once or multiple times
5 X {n} Must appear n times 6 X {n ,} Must appear more than n times
7 X {n, m} N ~ must appear ~ MB

 

 

 

  Logical operators (X and Y indicate a set of specifications)
 No. Specifications Description No. Specifications Description
1 XY Followed by the Y Specification 2 X | Y X or Y
3 (X) As a capture group Specification

 

 

 

  Common Pattern class methods
No. Method Type Description
1 Public static Pattern compile (String regex) Normal Regular Expression rules
2 Public Matcher (CharSequence input) Normal Returns a Matcher class instance.
3 Public String [] split (CharSequence input) Normal String splitting
In the Pattern class, if you want to obtain the Pattern class instance, you must call the compile () method.  Common Matcher class methods
No. Method Type Description
1 Public boolean matches () Normal Perform verification
2 Public String replaceAll (String replacement) Normal String replacement
If you want to verify whether a string complies with the specifications, you can use Matcher Class 3, regular expression verification 3.1, validation character Composition
Package com. pb. demo2; import java. util. regex. pattern; public class RegexDemo1 {public static void main (String [] args) {String str = "1234567890"; // declare that the String is composed of digits. // use regular expressions to verify if (Pattern. compile ("[0-9] + "). matcher (str ). matches () {System. out. println ("composed of numbers. ");} Else {System. out. println (" is not all composed of numbers. ");}}}
3.2 verify whether a string is in valid date format

 

Package com. pb. demo2; import java. util. regex. matcher; import java. util. regex. pattern;/** verify whether a String is in valid date format */public class RegexDemo2 {public static void main (String [] args) {String str = "1983-07-23 "; string pat = "\ d {4}-\ d {1, 2}-\ d {1, 2}"; // define rule Pattern p = Pattern. compile (pat); // instantiate the pattern Class Object Matcher m = p. matcher (str); // verify whether the string content is legal // use regular expression to verify if (m. matches () {System. out. println ("the date format is valid! ! ");} Else {System. out. println (" the date format is invalid! ! ");}}}
3.3 split the string according to the number of the string
Package com. pb. demo2; import java. util. regex. matcher; import java. util. regex. pattern;/** split the String according to the number of the String */public class RegexDemo2 {public static void main (String [] args) {String str = "a1b22c333d44e5555555f6g77 "; string pat = "\ d +"; // defines the sharding rule Pattern p = Pattern. compile (pat); // instantiate the Pattern class String [] ss = p. split (str); // split it into a String array // traverse for (String s: ss) {System. out. print (s + "\ t ");}}}

Result:

A    B    C    D    E    F    G    
3.4 replacement
Package com. pb. demo2; import java. util. regex. matcher; import java. util. regex. pattern;/** Replace the String with the number of the String */public class RegexDemo4 {public static void main (String [] args) {String str = "a1b22c333d44e5555555f6g77 "; string pat = "\ d +"; // defines the replaced rule Pattern p = Pattern. compile (pat); // instantiate the Pattern class Matcher m = p. matcher (str); // instantiate the Matcher class String newStrirng = m. replaceAll ("_"); // Replace the character System. out. println (newStrirng );}}

Result:

A_B_C_D_E_F_G_
3.5 email Verification
Package com. pb. demo2; import java. util. imports; import java. util. regex. matcher; import java. util. regex. pattern;/** whether the mailbox is valid */public class RegexDemo5 {public static void main (String [] args) {custom input = new role (System. in); // String pat = "^ [A-Za-z0-9 _] + @ [A-Za-z0-9 _] + (. [A-Za-z _] {2, 3}) {1, 2} $ "; String regEx =" ^ \ w + (. [A-Za-z _] {2, 3}) {1, 2} $ "; System. out. println ("input email:"); String email = input. nextLine (); Pattern p = Pattern. compile (regEx); Matcher m = p. matcher (email); if (m. matches () {System. out. println ("the email address is valid !! ");} Else {System. out. println (" the email address is invalid !! ");}}}
4. Regular Expressions supported by String: The following three methods are supported in the String class:
No. Method Type Description
1 Public boolean matches (String regex) Normal String Matching
2 Public String replaceAll (String regex, String replacement) Normal String replacement
3 Public String [] split (String regex) Normal String splitting
 

 

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.