Regular Expressions--Intelligent Podcast Learning _ Regular Expressions

Source: Internet
Author: User
Tags ming
With a little achievement, the basic function of Gobang game has been achieved almost, but also need to do some code optimization, look forward to the arrival of tomorrow, because tomorrow I can realize my dream, that is the night of their own development of the game, hehe! Learn in it, enjoy it! Afternoon really some waist sour back pain, decided to go to the nearest mountain, after deliberation decided to go to Baiwangshan Forest Park, really is a long time no exercise, just more than 200 meters of mountain climb back tired not, but also feel a bit of nature's amorous feelings, standing on the top of the mountain feel really good, looking at the mountain scenery, suddenly feel so small, This city is so big, when and where is my place of refuge? Efforts, I believe in the near future, I believe my choice. After dinner, looking at the video of Zhang Xiaoxiang: 's servlet, the servlet was prepared for the basic preview. Here are the results of today's study and share with you.
1. What is a regular expression
A regular expression is a formula that matches a type of string with a pattern. The individual feels like a string of characters that defines a pattern for searching for a matching string.
2. The application of regular expressions in strings
Note: Because the basic rules are relatively simple, and the API and the network have an unknown solution, the following main summary of the application in the string
Regular expressions are mainly used in strings, functions have matching, searching, cutting, replacing
1) Matching
This is done primarily through the Mathes method of the string class, as long as the defined regular expression is passed in as a parameter.
Example: Match string "123456789012345" is QQ number
String QQ = "123456789012345";
String Qqreg = "[1-9]\\d{4,14}";
System.out.print (Qq.matches (Qqreg));
2) Search
Establish regular rules, encapsulate rules into objects (compile of pattern), Act on Strings (Matcher), return a match, and use the matching function string to find the desired string.
Example: Remove 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 Textreg = "\\b[a-z]{2}\\b";//Take out two-letter words;
Pattern p = pattern.compile (Textreg);//The regular rule is encapsulated as an object.
Matcher m = p.matcher (text);//Get the match through the regular object. Let the rule act on the string. while (M.find ())
System.out.println (M.group ());
3) Cutting
Through the split method to achieve
Example: the string
Copy Code code as follows:

String str = "Sazzdkqqqqlfooojsz";
String Strreg = "(.) \\1+ ";//" \ ";

string[] arr = Str.split (Strreg);

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

4) Replacement
Implemented by Repalceall method of string
Example: To 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");//Make up two 0 in front of each paragraph.
ip = Ip.replaceall ("0+ (\\d{3})", "$"); Keep only the last three digits of each paragraph.
string[] Iparr = Ip.split ("");
treeset<string> ts = new treeset<string> ();//Because there are many IP addresses that need to be stored in containers and need to be sorted, TreeSet
for (String I:iparr) {ts.add (i);}
for (String i:ts) {System.out.println (I.replaceall ("0* (\\d+)", "$");}

3. Regular expressions of common use
1 A regular expression that matches a blank row: \n\s*\r
2) matching the regular expression of the end to end whitespace characters: ^\s*¦\s*$
3 A regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
4 A regular expression matching URL URLs: [a-za-z]+://[^\s]*
5 Match account number is legitimate (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$
6 Match China ZIP Code: [1-9]\d{5} (?!) \d)
7) Matching ID: \d{15}¦\d{18}
Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]
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.