Use regular expressions to send intelligence to podcasts for learning

Source: Internet
Author: User

With a little achievement, the basic functions of wuziqi games have been almost achieved, and some code optimization is required. We look forward to tomorrow, because tomorrow I will be able to realize my dream, that is the game developed by myself at night! Learn and enjoy it! In the afternoon, I decided to climb a mountain in the neighborhood. After deliberation, I decided to go to the baiwangshan Forest Park. It was really a long time that I had not exercised. I was tired when I climbed the mountain over two hundred meters, however, I felt the natural style and the feeling of standing on the top of the hill was really good. Looking at the scenery below the hill, I suddenly felt so small that the city was so big, when and where is my security? I believe that in the near future, I believe in my choice. After dinner, I watched a video from Teacher Zhang Xiaoxiang's Servlet and previewed the Servlet. Let's share with you the results of today's learning.
1. What is a regular expression?
A regular expression is a formula used to match a type of strings in a certain pattern. I personally think it is a string consisting of a character. It defines a pattern for searching and matching strings.
2. Application of Regular Expressions in strings
Note: because the basic rules are relatively simple and there is no detailed explanation in the API and network, the following mainly summarizes the application in the string.
Regular Expressions mainly act on strings and have functions such as matching, searching, cutting, and replacement.
1) Matching
This is mainly implemented through the mathes method of the String class. You only need to pass the defined regular expression as a parameter.
For example, if the matching string "123456789012345" is a QQ number
String qqqq = "123456789012345 ";
String qqreg = "[1-9] \ d {4, 14 }";
System. out. print (qq. matches (qqreg ));
2) Search
Create a regular rule, encapsulate the rule into an object (compile of Pattern), act on a string (Matcher), return a Matcher, and use a Matcher to find the desired string)
For example, retrieve the string "ming tian wu zi qi jiu kai fa wan le! "Two-letter words
String text = "ming tian wu zi qi jiu kai fa wan le !! ";
String texreg = "\ B [a-z] {2} \ B"; // retrieves a word consisting of two letters;
Pattern p = Pattern. compile (texreg); // encapsulate a regular rule into an object.
Matcher m = p. matcher (text); // obtain a Matcher from a regular object. Meaning: Let the rules apply to strings. While (m. find ())
System. out. println (m. group ());
3) Cutting
Implemented using the split method
For example
Copy codeThe Code is as follows:
String str = "sazzdkqqqqlfooojsz ";
String strreg = "(.) \ 1 + ";//"\\.";

String [] arr = str. split (strreg );

For (String s: arr)
{
System. out. println ("s =" + s );
}

4) replace
Implemented through the string repalceAll Method
For example, store "10.10.10.10 192.168.105.22 1.1.1.1 2.2.2.2 211.68.43.254" by network segment
String ip = "10.10.10.10 192.168.105.22 1.1.1.1 2.2.2.2 211.68.43.254 ";
Ip = ip. replaceAll ("(\ d +)", "00 $1"); // Add two zeros before each segment.
Ip = ip. replaceAll ("0 + (\ d {3})", "$1"); // only the last three digits of each segment are retained.
String [] ipArr = ip. split ("");
TreeSet <String> ts = new TreeSet <String> (); // because there are many IP addresses, you need to store them in containers and sort them.
For (String I: ipArr) {ts. add (I );}
For (String I: ts) {System. out. println (I. replaceAll ("0 * (\ d +)", "$1 "));}

3. Common Regular Expressions
1) Regular Expression matching blank rows: \ n \ s * \ r
2) Regular Expression matching the first and last blank characters: ^ \ s * bytes \ s * $
3) Regular Expression matching the Email address: \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*
4) Regular Expression matching URL: [a-zA-z] +: // [^ \ s] *
(5) Whether the matched account is valid (starting with a letter, may be 5-16 bytes, may be letters and numbers underline): ^ [a-zA-Z] [a-zA-Z0-9 _] {} $
6) match the Chinese zip code: [1-9] \ d {5 }(?! \ D)
7) matched ID card: \ d {15} found \ d {18}
Regular Expression matching Chinese characters: [\ u4e00-\ u9fa5]

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.