Dark Horse Programmer -- [Java Basics] -- Regular Expressions, java Regular Expressions

Source: Internet
Author: User

Dark Horse Programmer -- [Java Basics] -- Regular Expressions, java Regular Expressions

---------- Android training, java training, and hope to communicate with you! ----------

I. Overview

1. concept: an expression that complies with certain rules.

2. function: used to operate strings.

3. Features: some code operations are expressed with some specific symbols, which can simplify writing.

4. Benefits: You can simplify the complex operations on strings.

5. Disadvantages: the more definitions are met, the longer the regular expression, and the worse the readability.

Ii. Common rules

1, Character class

[Abc]: a, B, or c

[^ Abc]: represents any character except a, B, or c.

[A-zA-Z]: indicates a to z or A to Z

[A-d [m-p]: indicates a to d or m to p ([a-dm-p] (union ))

[A-z & [def]: d, e, or f (intersection)

[A-z & [^ bc]: represents a to z, except for B and c: [ad-z] (minus)

[A-z & [^ m-p]: indicates a to z, rather than m to p: [a-SCSI-z] (minus)

2And predefined characters

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

\ D: Number: [0-9]

\ D: Non-digit: [^ 0-9]

\ S: white space character: [\ t \ n \ x0B \ f \ r]

\ S: Non-blank characters: [^ \ s]

\ W: word character: [a-zA-Z_0-9]

\ W: Non-word character: [^ \ w]

3, Boundary match

^: Start of a row

$: End of a row

\ B: Word boundary

\ B: Non-word boundary

\ A: Start of input

\ G: End of the previous match

\ Z: The end of the input. It is only used for the final terminator (if any)

\ Z: End of input

4,GreedyQuantifiers

X? : X does not appear once or once

X *: X appears zero or multiple times

X +: X appears once or multiple 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, Group, and capture

The capture group can be numbered from left to right by calculating its parentheses. For example, in expression (A) (B (C), there are four such groups:

1 (A) (B (C )))

2 \

3 (B (C ))

4 (C)

Group zero always represents the entire expression, and $ matches the group content frequently in replacement.

Iii. Common functions of Regular Expressions

Common functions of regular expressions include matching, cutting, replacement, and obtaining.

1, Match

The boolean matches (String regex) method in the String class. Match the entire string with the rule. If one of them does not match the rule, the match ends and false is returned.

2Cutting

String [] split (String regex) method in the String class.

3Replacement

String replaceAll (String regex, String replacement) method. Example:

String str = "zhangsantttxiaoqiangmmmzhan"

Str = str. replaceAll ("(.) \",);

4, Get

Obtain: the sub-string that matches the rules in the string.

Procedure:

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

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

(3) obtain the regular expression matching engine after Association.

(4) perform operations on sub-strings that comply with the rules through the engine, such as searching and retrieving. M. find (), m. group ();

4. Apply Regular Expression exercises

Exercise 1: String Conversion

1/* requirement: Convert the following string to: I want to learn programming 2 "I am... I .. I want... yes... to .... learning .... learning ...... editing... cheng... cheng .... "3 4 train of thought: 5 convert an existing string into another string. Use the replacement function. 6. Remove. 7. Change multiple duplicate content into a single content. 8 */9 class ReplaceTest {10 public static void main (String [] args) {11 String s = "I am... I .. I want... yes... to .... learning .... learning ...... editing... cheng... cheng .... "; 12 System. out. println (s); 13 14 String regex = "\\. + "; // set. remove 15 s = s. replaceAll (regex, ""); // remove. 16 System. out. println (s); 17 18 regex = "(.) \ 1 + "; // convert the duplicate content into a single content 19 s = s. replaceAll (regex, "$1"); // deduplication 20 System. out. println (s); 21} 22}

 

  Exercise 2: Change the ip addressThe addresses are sorted in the order of CIDR blocks.

1/* requirement: sort the IP address segments in sequence. 2 192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.301 3 4 train of thought: 5 also follow the natural string order, as long as each of them is 3 bits. 6. Perform the completion based on the maximum number of zeros required by each segment. Therefore, each segment must have at least three digits. 7 2. Retain only three digits for each segment. In this way, all IP addresses are 3 bits per segment. 8 */9 import java. util. *; 10 class IPSortTest {11 public static void main (String [] args) {12 String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.301"; 13 System. out. println (ip); 14 15 String regex = "(\ d +)"; 16 ip = ip. replaceAll (regex, "00 $1"); // ensure that each segment has at least three digits ------------- 17 System. out. println (ip); 18 19 regex = "0 * (\ d {3})"; 20 ip = ip. replaceAll (regex, "$1"); // each segment retains only three 21 systems. out. println (ip); 22 23 regex = ""; 24 String [] arr = ip. split (regex); // split by space to 25 26 // define a TreeSet set and use the elements to naturally sort 27 TreeSet <String> ts = new TreeSet <String> (); 28 for (String str: arr) {29 ts. add (str); // add 30} 31 regex = "0 * (\ d)"; // Replace the excess 0 in front of each segment with 32 for (String s: ts) {33 System. out. println (s. replaceAll (regex, "$1"); // Replace the excess 0 in front of each segment with 34} 35} 36}

 

  Exercise 3: Email address verification

1 // requirement: Check the email address. 2 class CheckMail {3 public static void main (String [] args) {4 String mail = "123a809bc@sina.com.cn "; 5 String regex = "\ w + @ [a-zA-Z0-9] + (\\. [a-zA-Z] +) {1, 3} "; // more accurate 6 regex =" \ w + (\\. \ w +) + "; // relatively inaccurate matching. 7 boolean B = mail. matches (regex); 8 System. out. println (B); 9} 10}

 

  Exercise 4: Web Crawler

/* Web crawler is actually a function used to collect specified information requirements on the Network: it can be used to collect information such as mailbox and QQ number. */Import java.net. *; import java. util. regex. *; import java. io. *; class Spider {public static void main (String [] args) throws Exception {// getFileMail (); getWebMail ();} // get mail public static void getWebMail () throws Exception {// encapsulate the webpage URL url = new URL ("http://tieba.baidu.com/p/1390896758"); // connect to the server URLConnection conn = url. openConnection (); // a webpage with a buffer zone reads a stream BufferedReader br = new BufferedReader (new I NputStreamReader (conn. getInputStream (); String line = null; // defines the regular expression String regex = "\\ w + \ w + (\\. \ w +) + "; Pattern p = Pattern. compile (regex); // encapsulate Regular Expressions // read webpage data while (line = br. readLine ())! = Null) {// Matcher m = p. matcher (line); // find the matching Email Address while (m. find () {System. out. println (m. group (); // output matched email }}// obtain the email address in the specified document. Use the get function. Pattern Matcher public static void getFileMail () throws Exception {// encapsulate the File into an object file File = new File ("E: \ Java Study \ Practice \ day25 \ mail.txt "); // creates a buffer-based read stream BufferedReader br = new BufferedReader (new FileReader (file )); string line = null; // define the regular expression String regex = "\ w + @ [a-zA-Z] + (\\. [a-zA-z] +) + "; // create a Pattern object and encapsulate the Regular Expression Pattern p = Pattern. compile (regex); // read the data in the file while (line = br. readLine ())! = Null) {// stream-off string Matcher m = p. matcher (line); while (m. find () // find the matched string {System. out. println (m. group (); // output matched string }}}}

 

---------- Android training, java training, and hope to communicate with you! ----------


[2012 dark horse programmer] JAVA basics 02 [Top] rar and [2012 dark horse programmer] JAVA basics 02 [bottom] rar decompression Password

Www.itheima.com/main/feature/bxd_25.shtml
Download the password for the last two days.

Other: www.itheima.com/main/studyline/heimaline.html
Hope to help you

Ask who has a full set of java teaching videos for black horse programmers, Zhang Xiaoxiang and Bi Xiangdong

Edu.csdn.net/main/video.shtmlpackage you are satisfied with, from basic to advanced, there is no missing. Hope you can make good use of it! At the same time, I wish you the best effort for the development of the Chinese software industry!

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.