2016-04-07 9 views
2

C#では、goステートメントが行の唯一のステートメントであり、直前にはuse $(trop)ステートメントではない正規表現を作成しようとしています。貪欲でない否定的な裏返し

goのステートメントはすべて見つかっていますが、私はそれを正しく取得できません。最初のものを見つけています(下に「これをキャッチしないでください」)。負のlookbehindを正しく動作させる。私が間違っていることを誰かが助けてくれますか?

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 
+1

なし 'C#が'それは、SQL ServerのT-SQLのように見える、ではありません。また、あなたの質問が正規表現についてのものであれば、私は 'regex'で投稿にタグを付けて、必要なヘルプを得る機会を増やすことができます。 – gmiley

+0

件名に言語を記載する必要もありません。これをチェックしてください:http://stackoverflow.com/help/how-to-ask –

+0

みんな、これは彼のコードだとは思わない。私はこれが彼が解析しようとしている例だと思う。 – Icemanind

答えて

1

はい、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(); 
} 
+0

ありがとう!これはトリックでした。 –

0

あなたはこれを使用することができるはずです::

(?<!(use \$\(trop\)[\r\n]))^go$ 

これは、少なくとも、あなたの例で動作

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

は、ここで私が探していたテキストです。

+0

ありがとう - しかし、これはC#で動作しませんでした:( –

関連する問題