Java starts from zero 32 (regular expression)

Source: Internet
Author: User

First, why have regular regular expressions can easily match the data, you can perform more complex string verification, split, replace the function. For example, it is now required to determine whether a string is composed of numbers, and there are two ways to do this: do not use regular completion two,Pattern, Matcher classIf you want to apply a regular expression to your program, you must rely on the pattern class and the Matcher class, which are defined in the Java.util.regex package. Two classes. The main function of the pattern class is to write the regular specification, and the Matcher class is the execution specification, which verifies whether a string conforms to its specification. Common Regular Rules
No. Specification Describe No. Specification Describe
1 \\ Represents a backslash (\) character 2 \ t Represents a tab character
3 \ n Represents a line break 4 [ABC] Character A, b, or C
5 [^ABC] Any character except A, B, c 6 [A-za-z0-9] denoted by letters, numbers
7 \d Represents a number 8 \d Represents non-numeric
9 \w Denotes letters, numbers, underscores 10 \w Denotes non-alphabetic, numeric, underline
11 \s Represents all whitespace characters (line breaks, spaces, and so on) 12 \s Represents all non-whitespace characters
13 ^ The beginning of the line 14 $ End of Line
15 . Match any character except line break

  Quantity Representation (x represents a set of specifications)
No. Specification Describe No. Specification Describe
1 X Must appear once 2 X? can occur 0 or 1 times
3 x* can occur 0 times, 1 times or more 4 x+ Can occur 1 or more times
5 X{n} Must appear n times 6 X{n,} Must appear more than n times
7 X{N,M} Must appear n~m times

logical operators (X, y represent a set of specifications)
No. Specification Describe No. Specification Describe
1 Xy x specification followed by Y specification 2 X | Y x specification or Y specification
3 X As a capture group specification

common methods of pattern class
No. Method Type Describe
1 public static Pattern compile (String regex) Ordinary Specifying regular expression rules
2 Public Matcher Matcher (charsequence input) Ordinary Returns an instance of the Matcher class
3 Public string[] Split (charsequence input) Ordinary String splitting
in the pattern class, if you want to get an instance of the pattern class, you must call the compile () method. Common methods of Matcher class
No. Method Type Describe
1 public Boolean matches () Ordinary Perform validation
2 public string ReplaceAll (string replacement) Ordinary String substitution
If you want to verify that a string conforms to the specification, you can use the Matcher class three, the regular validation 3.1, the validation character composition
 PackageCom.pb.demo2;ImportJava.util.regex.Pattern; Public classRegexDemo1 { Public Static voidMain (string[] args) {String str= "1234567890";//the declaration string is made up of numbers//using regular validation        if(Pattern.compile ("[0-9]+"). Matcher (str). Matches ()) {System.out.println ("Numbers. "); }Else{System.out.println ("Not all are made up of numbers. "); }            }}
3.2. Verify whether a string is a valid date format

 PackageCom.pb.demo2;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** Verify if a string is a valid date format*/ Public classRegexDemo2 { Public Static voidMain (string[] args) {String str= "1983-07-23"; String Pat= "\\d{4}-\\d{1,2}-\\d{1,2}";//Defining RulesPattern P=pattern.compile (PAT);//instantiating a pattern class objectMatcher M=p.matcher (str);//Verify string content is legitimate//using regular validation        if(M.matches ()) {System.out.println ("Date format is legal!!"); }Else{System.out.println ("Date format is not legal!!"); }                    }}
3.3. Split the string according to the number of the string
 PackageCom.pb.demo2;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** Split the string according to the number of the string*/ Public classRegexDemo2 { Public Static voidMain (string[] args) {String str= "A1b22c333d4444e55555f6g77"; String Pat= "\\d+";//define a split rulePattern P=pattern.compile (PAT);//instantiating the pattern classString [] ss=p.split (str);//split into string arrays//Traverse         for(String s:ss) {System.out.print (s+ "\ T"); }            }}

Results:

A    B    C    D    E    F    G    
3.4. Replace Operation
 PackageCom.pb.demo2;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** Replace the string with the number of the string*/ Public classRegexDemo4 { Public Static voidMain (string[] args) {String str= "A1b22c333d4444e55555f6g77"; String Pat= "\\d+";//define the rules for substitutionPattern P=pattern.compile (PAT);//instantiating the pattern classMatcher M=p.matcher (str);//instantiate the Matcher classString Newstrirng=m.replaceall ("_");//the character to replaceSystem.out.println (NEWSTRIRNG); }}

Results:

A_b_c_d_e_f_g_
3.5. Email Verification
 PackageCom.pb.demo2;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** Email is legal*/ Public classRegexDemo5 { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); //String pat= "^[a-za-z0-9_][email protected][a-za-z0-9_]+ (. [ a-za-z_]{2,3}) {1,2}$ ";String regex= "^\\[email protected]\\w+ (. [ a-za-z_]{2,3}) {1,2}$ "; System.out.println ("Enter mailbox:"); String Email=Input.nextline (); Pattern P=Pattern.compile (regEx); Matcher m=p.matcher (email); if(M.matches ()) {System.out.println ("Legal Email!! "); }Else{System.out.println ("The mailbox is illegal!! "); }            }}
Fourstring support for regular expressionsThe following three methods are available in the string class to support a regular operation
No. Method Type Describe
1 Public boolean matches (String regex) Ordinary String match
2 public string ReplaceAll (string regex,string replacement) Ordinary String substitution
3 Public string[] Split (String regex) Ordinary String splitting

Java starts from zero 32 (regular expression)

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.