Java-based 7-Regular Expressions and java-based Regular Expressions
Java basics 7-Regular Expression 1. Definition:
Combination of specific symbols
Ii. role:
Used to operate string data
Iii. Advantages and Disadvantages
Simplified code, but poor readability
Iv. Introduce question 4.1
Determine if a number is a QQ number?
Not starting with zero
6-15 digits
Only contain numbers
Code 4.2
1 int len = qq. length (); 2 3 if (len> = 5 & len <= 15) {4 5 if (! Qq. startsWith ("0") {6 try {7 long l = Long. parseLong (qq); 8 9 System. out. println (l + ": Correct"); 10} catch (NumberFormatException e) {11 System. out. println (qq + ": contains illegal characters"); 12} 13 14} else {15 System. out. println (qq + ": cannot start with 0"); 16} 17} else {18 System. out. println (qq + ": incorrect length"); 19} 20}
4.3 key points
If (! Qq. startsWith ("0 "))
Starts with 0.
Long l = Long. parseLong (qq );
All judgments are numbers.
5. Use regular expressions to introduce code 5.1
String regex = "[1-9] [0-9] {4, 14}"; // regular expression.
Boolean B = qq. matches (regex );
System. out. println (qq + ":" + B );
5.2 description
String regex = "[1-9] [0-9] {4, 14 }";
First Digit [1-9]
Second digit [0-9]
[0-9] {4, 14} repeats 4-14 times later
Brackets indicate values, braces indicate the number of times, and parentheses indicate groups.
Vi. constructor of Regular Expressions
Java. util. regex
In the Pattern class
VII. Regular Expression constructor summary 7.1 character classes
[Abc] a, B, or c (simple class)
[^ Abc] any character except a, B, or c (NO)
[A-zA-Z] letters from a to z or from A to Z are included in the range)
[A-d [m-p] a to d or m to p: [a-dm-p] (union)
[A-z & [def] d, e, or f (intersection)
[A-z & [^ bc] a to z, except for B and c: [ad-z] (minus)
[A-z & [^ m-p] a to z, instead of m to p: [a-SCSI-z] (minus)
7.2 characters
X characters x
\ Backslash character
7.3 pre-defined character class
. Any character (may or may not match the line terminator)
\ D Number: [0-9]
\ D non-numeric: [^ 0-9]
\ S blank character: [\ t \ n \ x0B \ f \ r]
\ S non-blank characters: [^ \ s]
\ W word character: [a-zA-Z_0-9]
\ W non-word characters: [^ \ w]
7.4 Greedy quantifiers
X? X, neither once nor once
X * X, zero or multiple times
X + X, 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
7.5 Border Matters
^ Beginning of a row
$ End of a row
\ B word boundary
\ B Non-word boundary
\
End of a match on \ G
The end of the \ Z input. It is only used for the final terminator (if any)
\ Z input end
8. Common Operations of regular expressions on Characters
* Common Operations of regular expressions on strings:
* 1. Match.
* The matches method in the String class is actually used.
*
* 2. Cut.
* Actually, the split method in the String class is used.
*
* 3. replace.
* Actually, the replaceAll () method in the String class is used.
*
* 4. obtain it.
* Encapsulate objects using regular rules.
* Pattern p = Pattern. compile ("a * B ");
* // Associate with the matcher method string of the regular object. Obtains the Matcher object of the string operation.
* Matcher m = p. matcher ("aaaaab ");
* // Operate the string using the Matcher object method.
* Boolean B = m. matches ();
8.1 matching
// Check whether the matching mobile phone number is correct.
String tel = "15800001111 ";
String regex = "1 [358] \ d {9 }";
Boolean B = tel. matches (regex );
System. out. println (tel + ":" + B );
8.2 Cutting
Split the names in the string
String str = "zhangsanttttxiaoqiangmmmmzhaoliu ";
String [] names = str. split ("(.) \ 1 +"); // str. split ("\\.");
For (String name: names ){
System. out. println (name );
}
Parentheses are groups, (.) is the first group, \ 1 is \ 1, representing the first group, that is, reuse, + represents in turn or multiple times
"" To be split with spaces
"#" To # Cut
If the string contains multiple spaces "+" and multiple spaces
8.3 replace
Replace multiple letters with one
String str = "zhangsanttttxiaoqiangmmmmzhaoliu ";
Str = str. replaceAll ("(.) \ 1 +", "$1 ");
System. out. println (str );
Replace the four digits in the middle of the phone number with the "*" sign.
String tel = "15800001111"; // 158 *** 1111;
Tel = tel. replaceAll ("(\ d {3}) \ d {4} (\ d {4})", "$1 ***** $2 ");
System. out. println (tel );
("(.) \ 1 +", "$1") $1 is the first set of data with the first parameter, $ is to reference the previous Parameter
In this way, you can replace multiple t and six m values with one
("(\ D {3}) \ d {4} (\ d {4})", "$1 ***** $2 ") keep the head and end of the phone number. I just need to replace them with four * numbers in the middle.
(\ D {3}) Repeat the number three times and put it in the first group.
8.4 obtain
Returns three characters of a string.
String str = "da jia hao, ming tian bu fang jia! ";
String regex = "\ B [a-z] {3} \ B ";
// 1. encapsulate the regular expression into an object.
Pattern p = Pattern. compile (regex );
// 2. Obtain the matching object through the regular object.
Matcher m = p. matcher (str );
// Use the Matcher object method to operate the string.
// Since you want to get a word consisting of three letters
// Search. Find ();
System. out. println (str );
While (m. find ()){
System. out. println (m. group (); // obtain the matched subsequence.
System. out. println (m. start () + ":" + m. end ());
}
"\ B [a-z] {3} \ B", \ B Represents the character boundary, and [a-z] {3} represents three lower-case letters
Three steps
9. Regular Expression instance
* 1. Treating stuttering: I am... I am... I want... to be... yes .. learning... learn to compile... compile and compile .. compile .. cheng... cheng... cheng
* 2. Sort IP addresses.
* 3. Verify the email address.
9.1 treating stuttering
String str = "I am... I am... I want... to be... yes .. learning... learn to compile... compile and compile .. compile .. cheng... cheng... cheng ";
// 1, remove. From the string. Replace.
Str = str. replaceAll ("\. + ","");
System. out. println (str );
// 2. Replace the overlapping words.
Str = str. replaceAll ("(.) \ 1 +", "$1 ");
System. out. println (str );
9.2 sort IP addresses
String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55 ";
// 1. In order to allow the ip address to be compared in string order, as long as the number of digits in each segment of the ip address is the same.
// Therefore, add zero and add multiple zeros for each digit. Add two zeros to each segment.
Ip_str = ip_str.replaceAll ("(\ d +)", "00 $1 ");
System. out. println (ip_str );
// Each segment is retained with three digits.
Ip_str = ip_str.replaceAll ("0 * (\ d {3})", "$1 ");
System. out. println (ip_str );
// 1. Switch the IP address out.
String [] ips = ip_str.split ("+ ");
TreeSet <String> ts = new TreeSet <String> ();
For (String ip: ips ){
// System. out. println (ip );
Ts. add (ip );
}
For (String ip: ts ){
System. out. println (ip. replaceAll ("0 * (\ d +)", "$1 "));
}
9.3 email address verification
String mail = "abc1@sina.com.cn ";
String regex = "[a-zA-Z0-9 _] + @ [a-zA-Z0-9] + (\. [a-zA-Z] {}) + ";
Regex = "\ w + @ \ w + (\. \ w +) +"; // 1@1.1
Boolean B = mail. matches (regex );
System. out. println (mail + ":" + B );