Black Horse Programmer--"Java Foundation"--Regular expression

Source: Internet
Author: User
Tags character classes

----------Android Training, Java training, look forward to communicating with you! ----------

I. Overview

1, the concept: conforms to certain rules the expression.

2. Function: Used to specifically manipulate strings.

3, Characteristics: with some specific symbols to represent some code operations, this can simplify writing.

4. Benefits: You can simplify the complex operation of strings.

5, the disadvantage: the more the definition, the longer the regular, the worse the reading.

Ii. common rules

1 , character classes

[ABC]: Denotes a, B, or C

[^ABC]: denotes any character except A, B, or C

[A-za-z]: denotes A to Z or a to Z

[A-d[m-p]]: represents a to D or M to P ([A-dm-p] (set))

[A-z&&[def]]:d, E, or F (intersection)

[A-Z&&[^BC]]: denotes A to Z, except B and C:[ad-z] (minus)

[A-z&&[^m-p]]: Represents a to Z, not M to P:[a-lq-z] (minus)

2 , predefined characters

. : Any character (may or may not match the line terminator)

\d: Number: [0-9]

\d: Non-numeric: [^0-9]

\s: White space characters: [\t\n\x0b\f\r]

\s: Non-whitespace characters: [^\s]

\w: Word character: [a-za-z_0-9]

\w: Non-word characters: [^\w]

3 , boundary match character

^: Beginning of line

$: End of line

\b: Word boundaries

\b: Non-word boundary

\a: Beginning of input

\g: End of last match

\z: End of input, only for final terminator (if any)

\z: End of input

4 , Greedy Quantity Words

X? : x does not appear once or once

X*:x appears 0 or more times

x+: X appears one or more times

X{n}:x exactly n times

X{n,}:x at least n times

X{n,m}x: At least n times, but not more than m times

5 , groups, and captures

Capturing groups can be numbered by calculating their opening brackets from left to right. For example, in an expression ((A) (B (C))), there are four such groups:

1 ((A) (B (C)))

2 \a

3 (B (C))

4 (C)

Group 0 always represents the entire expression, which in substitution is commonly used to match the contents of a group.

Iii. common functions of regular expressions

Regular expression common functions, there are four main types: matching, cutting, replacing and acquiring

1 , Matching

The Boolean matches (string regex) method in the String class. Matches the entire string with a rule, so long as there is a non-conforming rule, the match ends and false is returned.

2 , Cutting

The string[] Split (String regex) method in the String class.

3 , replace

String ReplaceAll (string regex, String replacement) method. Example:

String str = "Zhangsantttxiaoqiangmmmzhan"

str = Str.replaceall ("(.) \\” , );

4 , Get

Get: Is the substring of the string that matches the rule is taken out.

Operation Steps:

(1) Encapsulates a regular expression into an object. Pattern p = pattern.compile (regex);

(2) Associate the regular object with the string to be manipulated. Matcher m = p.matcher (str);

(3) After association, get the regular matching engine.

(4) operate through the engine on a substring that conforms to the rules, such as Find, remove. M.find (), M.group ();

Four, the regular expression application practice

Exercise 1 : String Conversion

1 /*requirements: Convert the following string into: I want to learn to program2 "I am ... I.. I'm going to... To... Want to .... Learn to learn .... Learn to learn ... To make a series of ... Ride... Regulation regulation .... "3 4 Ideas:5 changes an existing string to another string. Use the Replace function. 6 1, you can first remove. 7 2. Then turn multiple duplicate content into a single content. 8 */9 classreplacetest{Ten      Public Static voidMain (string[] args) { OneString s = "I am ... I.. I'm going to... To... Want to .... Learn to learn .... Learn to learn ... To make a series of ... Ride... Regulation regulation .... "; A System.out.println (s); -    -String regex = "\\.+";//first remove the. thes = S.replaceall (Regex, "");//removed. - System.out.println (s); -    -Regex = "(.) \\1+ ";//turn duplicate content into a single content +s = S.replaceall (Regex, "$");//Go heavy - System.out.println (s); +     } A}

  Exercise 2 : the IP addresses are sorted in order of address segments.

1 /*Requirements: Sort the IP addresses in the order of the address segments. 2 192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.3013 4 Ideas:5 also follow the natural order of the strings, so long as they are 3 bits each. 6 1, according to each paragraph needs the most 0 to make up, then each paragraph will be guaranteed at least 3. 7 2. Keep only 3 bits per paragraph. In this way, all IP addresses are 3 bits per segment. 8 */9 ImportJava.util.*;Ten classipsorttest{ One      Public Static voidMain (string[] args) { AString IP = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.301"; - System.out.println (IP); -    theString regex = "(\\d+)"; -ip = ip.replaceall (regex, "00$1");//ensure that each segment has at least three------------- - System.out.println (IP); -    +Regex = "0* (\\d{3})"; -ip = ip.replaceall (regex, "$");//only three bits are reserved per paragraph + System.out.println (IP); A    atRegex = ""; -string[] arr = ip.split (regex);//Cut by Space -          -         //defines a TreeSet collection that uses elements to sort naturally -treeset<string > ts =NewTreeset<string>(); -          for(String str:arr) { inTs.add (str);//Add -         } toRegex = "0* (\\d)";//Replace the extra 0 in front of each paragraph +          for(String s:ts) { -System.out.println (S.replaceall (Regex, "$"));//Replace the extra 0 in front of each paragraph the         } *     } $}

  Exercise 3 : Email Address verification

1 //requirements: Verify the email address. 2 classcheckmail{3      Public Static voidMain (string[] args) {4String mail = "[Email protected]";5String regex = "\\[email protected][a-za-z0-9]+ (\\.[ a-za-z]+) {1,3} ";//more precise6Regex = "\\[email protected]\\w+ (\\.\\w+) +";//a relatively less precise match. 7         Booleanb =mail.matches (regex);8 System.out.println (b);9     }Ten}

  Exercise 4 : web crawler

/*A web crawler is actually a feature that collects information about a specified information on a network: it can be used to collect messages such as mailboxes, QQ numbers, and so on. */Importjava.net.*;Importjava.util.regex.*;ImportJava.io.*;classspider{ Public Static voidMain (string[] args)throwsexception{//Getfilemail ();Getwebmail (); }      //get mail in Web page     Public Static voidGetwebmail ()throwsexception{//encapsulate Web page addressURL url =NewURL ("http://tieba.baidu.com/p/1390896758"); //connecting to a serverURLConnection conn =url.openconnection (); //page read stream with bufferBufferedReader br =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line=NULL; //define a regular expression that matches the e-mail addressString regex = "\\[email protected]\\w+ (\\.\\w+) +"; Pattern P= Pattern.compile (regex);//Encapsulating Regular Expressions//reading Web page data         while(line = Br.readline ())! =NULL){            //Regular Correlation DataMatcher m =P.matcher (line); //find a matching mailbox             while(M.find ()) {System.out.println (M.group ());//Output Matching mailbox            }        }    }      //gets the e-mail address in the specified document. Use the Get feature. Pattern Matcher     Public Static voidGetfilemail ()throwsexception{//encapsulate a file as an objectFile File =NewFile ("E:\\java study\\practice\\day25\\mail.txt"); //to create a read stream with a bufferBufferedReader br =NewBufferedReader (Newfilereader (file)); String Line=NULL; //Defining Regular ExpressionsString regex = "\\[email protected][a-za-z]+ (\\.[ a-za-z]+) + "; //creating pattern objects, encapsulating regular ExpressionsPattern p =pattern.compile (regex); //reading data from a file         while(line = Br.readline ())! =NULL){            //off-stream stringMatcher m =P.matcher (line);  while(M.find ())//look for a matching string {System.out.println (M.group ());//output a matching string            }        }    }}

----------Android Training, Java training, look forward to communicating with you! ----------

Black Horse Programmer--"Java Foundation"--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.