Remove carriage return and extra spaces _ regular expressions in SQL code through Java regular expressions

Source: Internet
Author: User
Copy Code code 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 results:
Copy Code code 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
As you can see, there is one that does not remove the extra space.

Visible Java in the regular style, the same meaning of the multiple writing, hehe! In fact, the main difference is the number of words matching patterns in mischief:

the original API document reads:

Greedy Quantity Word

X? X, not once or once
X* X, 0 or more times
x+ X, one or more 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 quantity Word

X?? X, not once or once
X*? X, 0 or more times
X+? X, one or more 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 Quantity Word

x?+ X, once or once there is no
x*+ X, 0 or more times
x + + x., one or more 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, there is no more detailed description of the three ways, in fact, the difference between the three modes is as follows:

Greedy: Try to find the longest match.
Reluctant: Try to find the shortest match.
Possessive: Also try to find the longest match.

Although greedy and possessive force a matcher to read the entire text,greedy before making the first match, it often leads to multiple attempts to find a match, However possessive let a matcher try only one match at a time.

The following is a method in my tool:
[Code]
/**
* Determine if an SQL statement is already a paged SQL
*
* @param SQL Source sql
* @return is a paged SQL return ture, otherwise return false;
*/
public boolean isalreadysegmentsql (String sql) {
return Sql.replace (' \ R ', '). replace (' \ n ', '). ReplaceAll ("{2,}", ""). Matches ("(? i). +limit [\\d+ *|\\d *, *\\d+].+");
}

Or is the function tough Ah!

Add:
The following two-gaze regular formula is the same as the third semantics:
Copy Code code 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 comes from "Lava" blog
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.