使用 Regex 类型语法必须考虑的定义。
元字符
元字符指定用户可以在相应位置输入的字符。
| Character |
Descriptión |
. |
Any single character. |
[aeiou] |
Any single character from the specified character set. |
[^aeiou] |
Any single character not from the specified character set. |
[0-9a-fA-F] |
Any single character from the specified character range. |
\w |
Any single alphanumeric character. The same as [a-zA-Z_0-9]
|
\W |
Any single non-alphanumeric character. The same as [^a-zA-Z_0-9]
|
\d |
Any single numeric character. The same as [0-9]
|
\D |
Any single non-numeric character. The same as [^0-9]
|
量词
量词跟在元字符后面,并指定该字符应重复多少次。下表列出了可用的限定符。
| Quantifier |
Description |
Examples |
* |
Zero or more matches. The same as {0,}
|
[a-zA-Z]<em></em>, \w
|
+ |
One or more matches. The same as {1,}
|
[a-zA-Z]+, \w+
|
? |
Zero or one matches. The same as {0,1}
|
[a-zA-Z]?, \w?
|
{n} |
Exactly n matches. |
[0-9]{2} |
{n,} |
At least n matches. |
[0-9]{2,} |
{n,m} |
At least n, but not more than m matches. |
[0-9]{2,7} |
可以提供帮助的解决方案。
1。使用 Regex 从 SQL 语句中提取表名
正则表达式
/(from|join|into)\s+([`]*\w*.*\w[`]|(\[)*\w*.*(\])|\w*\.*\w*)/g
2。使用 Regex 从 SQL 语句中提取具有别名的表名
正则表达式
(from|join|into)\s+([`]*\w*.*\w[`] *\w*|(\[)*\w*.*(\]) *\w*|\w*\.*\w* *\w*)
3。使用 Regex 从 SQL 语句中提取列名
正则表达式
/(\w*\.*\w+|`\w*.*\w`|(\[)\w*.*(\]))+(,|\s+,|\s+FROM|\s+from)/g
为 C# 生成的代码
public static class QueryExtension
{
public static List<string> GetTables(this string query)
{
List<string> tables = new List<string>();
string pattern = @"(from|join|into)\s+([`]*\w*.*\w[`]|(\[)*\w*.*(\])|\w*\.*\w*)";
foreach (Match m in Regex.Matches(query, pattern))
{
string name = m.Groups[2].Value;
tables.Add(name);
}
return tables;
}
public static List<string> GetTablesWithAliases(this string query)
{
List<string> tables = new List<string>();
string pattern = @"(from|join|into)\s+([`]*\w*.*\w[`] *\w*|(\[)*\w*.*(\]) *\w*|\w*\.*\w* *\w*)";
foreach (Match m in Regex.Matches(query, pattern))
{
string name = m.Groups[2].Value;
tables.Add(name);
}
return tables;
}
public static List<string> GetColumns(this string query)
{
List<string> columns = new List<string>();
string pattern = @"(\w*\.*\w+|`\w*.*\w`|(\[)\w*.*(\]))+(,|\s+,|\s+FROM|\s+from)";
foreach (Match m in Regex.Matches(query, pattern))
{
string name = m.Groups[1].Value;
columns.Add(name);
}
return columns;
}
public static string Join(this IEnumerable<string> values, string separator) {
return string.Join(separator, values);
}
}
测试字符串
-------------------------------------------------------
select AuthorId, a.Name as [AuthorName], c.Name as City, s.Name as [State] from Author a
inner join `dbo`.`otherTable` ot on ot.col1 = a.Name
inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId
inner join City c on c.CityId = zc.CityId
inner join [State] s on s.StateId = c.StateId
-------------------------------------------------------
输出
//-------GetTables------
Author
`dbo`.`otherTable`
Zipcode
City
[State]
//-------GetTablesWithAliases------
Author a
`dbo`.`otherTable` ot
Zipcode zc
City c
[State] s
更多 C# 代码示例(此处)
https://stackoverflow.com/a/68889908/16731336
参考文献
Extract table names from an SQL statement with Regex
Regular Expression Language - Quick Reference
Simplified Regular Expressions