Remove line breaks and extra spaces in SQL code using Java Regular Expressions

Source: Internet
Author: User

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

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.