【发布时间】:2021-10-20 16:39:14
【问题描述】:
我有很多 sql 文件。我试图找到包含变量(格式为@varname)的文件,前提是它们出现在匹配的单引号或双引号中。我只关心它的存在和存在,我只需要知道发生这种情况的文件。
我可以匹配所有引用的字符串,但不知道如何测试即使只有一个 @ 字符出现在匹配项中
匹配单引号和双引号对(["'])(.*?)\1
示例文件:
...sql 语句
select @sql = '选择 * 来自
id = @id '的用户...更多 sql 语句
提前致谢。
编辑
这是一个更好的示例文件,其中包含语句应该匹配的 cmets (sql cmets) 以及不应该匹配的语句示例
...sql statements
-- only this quoted string would match
select @sql = "select * from
Users where id = @id "
-- other statements that wouldn't match because not in a pair of quotes
if ltrim(isnull(@stat,'')) <> '' and @stat <> '""'
begin
select @sql = @sql + " and Stat in ("+@stat+")"
end
if isnull(@atype,'') <> ''
begin
select @sql = @sql + " and Type in ("+@atype+")"
end
...more sql statements
【问题讨论】:
-
类似
(?s)(["'])((?:(?!\1)[^@])*@.*?)\1? -
感谢您的评论。那真的很近!它给出了一些误报,我已经更新了问题以包含一些不应该匹配的示例
-
多么令人兴奋的事情……试试
(?m)(?:^\h*--.*|(["'])(?:(?!\1)[^@])*\1)(*SKIP)(*F)|(?s)(["'])(?:(?!\2)[^@])*@.*?\2,见regex101.com/r/lS8F9f/1 -
你有时间检查吗?