Let's look at some examples:
Copy codeThe Code is as follows:
-- Obtain the count information of a table
Select count (*) from T with (nolock)
-- Obtain the count information of a specific value
Select count (*) from T with (nolock)
Where v = '-- value'
-- Obtain the count information of the table 'T'
Select count (*) from T with (nolock)
SELECT * from T -- get table T
Where P
Let's first try to give a simple match:
Copy codeThe Code is as follows:
\-[^ \ R \ n] * $
You will find that it matches the second SQL statement, which is incorrect. It seems that we should exclude the "'" And let's change it:
Copy codeThe Code is as follows:
\-[^ \ '\ R \ n] {0,} $
Still incorrect. Although the second SQL statement does not match, it does not even match the third SQL statement.
So how can we truly match all the SQL comments?
First, let's summarize some features of SQL Annotations:
1. Start --
2. The comment content should not be included in a pair''
3. The comment should only be at the end. Some statements can be selected before
Well, after collecting the above information, our final SQL annotation syntax also came out:
Copy codeThe Code is as follows:
\-([^ \ '\ R \ n] {0,} (\' [^ \ '\ r \ n] {0 ,}\') {0, 1} [^ \ '\ r \ n] {0,}) {0,} $
In this case, the comments of the four SQL statements are all matched. The regular expression is too powerful.
This SQL statement has a small problem, that is, it cannot be followed by a single quotation mark, otherwise there will be a matching problem. (Because people's habits use double separators by default, this small problem can be ignored)