gpt4 book ai didi

正则表达式仅在引号内匹配变量

转载 作者:行者123 更新时间:2023-12-04 07:14:42 33 4
gpt4 key购买 nike

我有很多 sql 文件。我试图定位包含变量(格式为 @varname)的文件,仅当它们出现在匹配的单引号或双引号内时。我只关心它是否存在并且在那里,我只需要知道发生这种情况的文件。
我可以匹配所有带引号的字符串,但无法弄清楚如何测试即使只有一个 @字符出现在匹配中
匹配单引号和双引号对 (["'])(.*?)\1示例文件:

...sql statements

select @sql = 'select * from
Users where id = @id '

...more sql statements


提前致谢。
编辑
这是一个更好的示例文件,其中包含关于哪些语句应该匹配的注释(sql 注释)和不应该匹配的示例
...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|=)(?:\"[^"]*@[^"]*\"|(?:\s|=)\'[^']*@[^']*\')
演示:
  • https://regex101.com/r/BXcYt4/1

  • enter image description here

    关于正则表达式仅在引号内匹配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68839911/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com