With a little achievement, the basic functions of Gozi Chess game have been almost realized, and some code optimization is needed. I look forward to tomorrow's arrival, because tomorrow I can realize my dream, that is, the game I developed by myself at night, haha! Learn and enjoy it! I had a sore waist and back pain in the afternoon, so I decided to go hiking nearby. After discussion, I decided to go to Baiwangshan Forest Park. I haven’t exercised for a long time. I was so tired after climbing the mountain that was only more than 200 meters long. However, I felt the charm of nature. Standing on the top of the mountain was really good. Looking at the scenery below the mountain, I suddenly felt so small. This city is so big. When and where is my place to stay? Work hard, I believe in the near future, I believe in my choice. After dinner, I watched the video of Teacher Zhang Xiaoxiang's Servlet and conducted a basic preview of the Servlet. Let me share with you today’s learning results.
1. What is a regular expression
A regular expression is a formula that uses a certain pattern to match a type of string. I personally think it is a string composed of characters, which defines a pattern used to search for matching strings.
2. Application of regular expressions in strings
Note: Since the basic rules are relatively simple, and there are no details in the API and on the network. The following mainly summarizes the application in strings
Regular expressions mainly act on strings, and their functions include matching, searching, cutting, and replacing.
1) Match
This is mainly implemented through the mathes method of the String class, as long as the defined regular expression is passed in as a parameter.
Example: Match the string "123456789012345" or not a QQ number
String qq = "123456789012345";
String qqreg = "[1-9]\\d{4,14}";
((qqreg ));
2) Search
Establish regular rules, encapsulate the rules into objects (Pattern's compile), act on strings (Matcher), return matchers, use matchers to act as strings, and find the required strings)
Example: Take out the two letter word "ming tian wu zi qi jiu kai fa wan le !"
String text = "ming tian wu zi qi jiu kai fa wan le !!";
String textreg = "\\b[a-z]{2}\\b";//Pick out the word composed of two letters;
Pattern p = (textreg);//Encapsulate regular rules into objects.
Matcher m = (text);//Get matcher through regular objects. Meaning: let the rules apply to the string. While(())
(());
3) Cutting
Implemented by split method
Example: Convert a string
String str = "sazzdkqqqqlfooojsz";
String strreg = "(.)\\1+";//"\\.";
String[] arr = (strreg);
for(String s : arr)
{
("s="+s);
}
4) Replacement
Implemented by the repalceAll method of the string
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 = ("(\\d+)","00$1");//Add two 0s in front of each paragraph.
ip = ("0+(\\d{3})","$1"); //Only the last three digits of each paragraph are retained.
String[] ipArr = (" ");
TreeSet<String> ts = new TreeSet<String>();//Because there are many IP addresses, it needs to be stored in containers and needs to be sorted. TreeSet
for(String i : ipArr){(i);}
for(String i : ts){(("0*(\\d+)","$1"));}
3. Commonly used regular expressions
1) Regular expression matching blank lines: \n\s*\r
2) Regular expression matching the beginning and end whitespace characters: ^\s* ¦\s*$
3) Regular expression matching the email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
4) Regular expression matching URL: [a-zA-z]+://[^\s]*
5) Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
6) Match the Chinese postal code: [1-9]\d{5}(?!\d)
7) Match ID card: \d{15} ¦\d{18}
Regular expression matching Chinese characters: [\u4e00-\u9fa5]
1. What is a regular expression
A regular expression is a formula that uses a certain pattern to match a type of string. I personally think it is a string composed of characters, which defines a pattern used to search for matching strings.
2. Application of regular expressions in strings
Note: Since the basic rules are relatively simple, and there are no details in the API and on the network. The following mainly summarizes the application in strings
Regular expressions mainly act on strings, and their functions include matching, searching, cutting, and replacing.
1) Match
This is mainly implemented through the mathes method of the String class, as long as the defined regular expression is passed in as a parameter.
Example: Match the string "123456789012345" or not a QQ number
String qq = "123456789012345";
String qqreg = "[1-9]\\d{4,14}";
((qqreg ));
2) Search
Establish regular rules, encapsulate the rules into objects (Pattern's compile), act on strings (Matcher), return matchers, use matchers to act as strings, and find the required strings)
Example: Take out the two letter word "ming tian wu zi qi jiu kai fa wan le !"
String text = "ming tian wu zi qi jiu kai fa wan le !!";
String textreg = "\\b[a-z]{2}\\b";//Pick out the word composed of two letters;
Pattern p = (textreg);//Encapsulate regular rules into objects.
Matcher m = (text);//Get matcher through regular objects. Meaning: let the rules apply to the string. While(())
(());
3) Cutting
Implemented by split method
Example: Convert a string
Copy the codeThe code is as follows:
String str = "sazzdkqqqqlfooojsz";
String strreg = "(.)\\1+";//"\\.";
String[] arr = (strreg);
for(String s : arr)
{
("s="+s);
}
4) Replacement
Implemented by the repalceAll method of the string
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 = ("(\\d+)","00$1");//Add two 0s in front of each paragraph.
ip = ("0+(\\d{3})","$1"); //Only the last three digits of each paragraph are retained.
String[] ipArr = (" ");
TreeSet<String> ts = new TreeSet<String>();//Because there are many IP addresses, it needs to be stored in containers and needs to be sorted. TreeSet
for(String i : ipArr){(i);}
for(String i : ts){(("0*(\\d+)","$1"));}
3. Commonly used regular expressions
1) Regular expression matching blank lines: \n\s*\r
2) Regular expression matching the beginning and end whitespace characters: ^\s* ¦\s*$
3) Regular expression matching the email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
4) Regular expression matching URL: [a-zA-z]+://[^\s]*
5) Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
6) Match the Chinese postal code: [1-9]\d{5}(?!\d)
7) Match ID card: \d{15} ¦\d{18}
Regular expression matching Chinese characters: [\u4e00-\u9fa5]