gpt4 book ai didi

c# - 非贪婪负后视

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:14 26 4
gpt4 key购买 nike

在 C# 中,我试图编写一个正则表达式来查找所有 go 语句,这些语句是该行中唯一的语句,并且前面没有紧跟 use $(trop) 语句。

我非常接近 - 它正在找到所有 go 语句,但我无法正确 - 它正在找到第一个(如下面标记为“不要捕获这个”) ,但我无法使负面回顾正常工作。谁能帮我解决我做错的事情?

(?<goStatement>^(\s{0,})go(\s{0,})$)(?<useTrop>(?<!(^\s{0,}use \(trop\)\s{0,}$)))`

这是我正在搜索的文本:

set noexec off
:setvar trop devtip
:setvar trop_wf_tracking trop_workflow_tracking
:setvar trop_wf trop_wf

-- Information - to set this script to only run once change stop_if_applied to 1.
:setvar stop_if_applied 1


-- Do NOT catch this one
use $(trop)
go



if $(stop_if_applied) = 1 and exists (select * from $(trop).dbo.DATABASE_VERSION where DB_SCRIPT_NUMBER = 20656) begin
select 'This db script has already been run. If it is required to run again change the variable stop_if_applied to 0. Disabling all further commands on the connection.'
,* from $(trop).dbo.DATABASE_VERSION
where DB_SCRIPT_NUMBER = 20656

set noexec on
return
end

-- DO catch this one ----------
go
-- ------------------------------------------------------------------------------

set xact_abort on

begin transaction

select * from $(trop).dbo.DATABASE_VERSION where DB_SCRIPT_NUMBER = 20656;

/* Insert your code here */

select * from dbo.SECURITY_RIGHT
-- DO catch this one ----------
go

/* End of your code here */
-- DO catch this one ----------
go

if not exists (select * from $(trop).dbo.DATABASE_VERSION where DB_SCRIPT_NUMBER = 20656)
insert into $(trop).dbo.DATABASE_VERSION (DB_SCRIPT_NUMBER, COMMENT)
values (20656, 'comment goes here');

select * from $(trop).dbo.DATABASE_VERSION where DB_SCRIPT_NUMBER = 20656;

commit

最佳答案

是的,lookbehind 应该在 ^go 之前进行。

 (?m)                              # Multi-line mode
(?<! use \s* \$\(trop\) \s* ) # Not a 'use $(trop)` behind
^ # Beginning of line
\s* go # The 'go'
[^\S\r\n]* # Optional horizontal whitespace
(?= \r? \n | $ ) # until the end of line or EOS

C# 测试

 Regex RxGo = new Regex(@"(?m)(?<!use\s*\$\(trop\)\s*)^\s*go[^\S\r\n]*(?=\r?\n|$)");
Match matchGo = RxGo.Match(sTarget);
while (matchGo.Success)
{
Console.WriteLine("'{0}'", matchGo.Value);
matchGo = matchGo.NextMatch();
}

关于c# - 非贪婪负后视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36487229/

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