Copy codeThe Code is as follows:
Public static void main (String [] args ){
String SQL = "SELECT * FROM \ n" +
"'Testdb'. 'foo' LIMIT 0,100 ";
String s = "SELECT * FROM 'testdb'. 'foo' LIMIT 0,100 ";
String sql2 = Pattern. compile ("{2,}"). matcher (s). replaceAll ("");
String sql3 = s. replaceAll ("{2 ,}","");
String sql4 = SQL. replace ('\ R ',''). replace ('\ n ',''). replaceAll ("{2 ,}","");;
String sql5 = SQL. replace ('\ R', ''). replace (' \ n',''). replaceAll ("{2 ,}? ","");;
String sql6 = SQL. replace ('\ R ',''). replace ('\ n ',''). replaceAll ("{2,} + ","");;
System. out. println (sql2 );
System. out. println (sql3 );
System. out. println (sql4 );
System. out. println (sql5 );
System. out. println (sql6 );
}
Output result:
Copy codeThe Code is as follows:
SELECT * FROM 'testdb'. 'foo' LIMIT 0,100
SELECT * FROM 'testdb'. 'foo' LIMIT 0,100
SELECT * FROM 'testdb'. 'foo' LIMIT 0,100
SELECT * FROM 'testdb'. 'foo' LIMIT 0,100
SELECT * FROM 'testdb'. 'foo' LIMIT 0,100
[Code]
Process finished with exit code 0
The result shows that there is no extra space removed.
It can be seen that the regular expression in java has multiple ways of writing the same meaning! In fact, the matching modes of different quantifiers are at odds:
The original API documentation states:
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
Reluctant 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
Possessive 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
However, the three modes are not described in detail. The differences between the three modes are as follows:
Greedy: Try to find the longest match.
Reluctant: Try to find the shortest match.
Possessive: Try to find the longest match.
Although greedy and possessive force a matcher to read the entire text before the first match, greedy often causes multiple attempts to find a match. However, possessive allows a matcher to only try a match once.
The following is a method in my tool:
[Code]
/**
* Determine whether an SQL statement is a paging SQL statement
*
* @ Param SQL source SQL
* @ Return: True is returned when a paging SQL statement is returned. Otherwise, False is returned;
*/
Public boolean isAlreadySegmentSQL (String SQL ){
Return SQL. replace ('\ R ',''). replace ('\ n ',''). replaceAll ("{2 ,}",""). matches ("(? I). + LIMIT [\ d + * | \ d *, * \ d +]. + ");
}
The regular expression function is powerful!
Supplement:
The following two regular expressions are the same as those in the third clause:
Copy codeThe Code is as follows:
// RegList. put ("(? I) bit \ ([2-9] \) \ z "," byte [] ");
// RegList. put ("(? I) bit \ (\ d {2, }\\) \ z "," byte [] ");
RegList. put ("(? I) bit \ (\ d {2,} | [2-9]) \ z "," byte [] ");
This article is from the "fuyan" blog