简体   繁体   中英

Visual Studio Regex find certain words

I am trying to use Visual Studio's Regex find in files. Basically, I have a database project that contains some .sql files which then contain CREATE PROCEDURE blocks.

I am looking to use a form of regular expression to only find .sql files that contain both "RAISERROR" and "COMMIT" statements in them. Unforunately, (?=\\bRAISERROR\\b)(?=\\bCOMMIT\\b) does not seem to find anything and any other variations seem to only retrieve either or as the result. I am not strong in regular expressions and I was unable to find a pattern that could perhaps possibly help with this.

Here's a sample procedure that should match that I am testing with

CREATE PROCEDURE [procedurename]

<irrelevant parameters>

AS
<TSQL code>

BEGIN TRANSACTION

<TSQL CODE>

RAISERROR('message', 16, 1)

    <TSQL code>

RAISERROR('another message', 16, 2)

    <TSQL code>

COMMIT

RETURN 0

Your attempt would not find anything. It looks for a zero length string that is immidately preceded by both RAISERROR and COMMIT. No such string can exist.

Instead you should search for RAISERROR which is followed at a later stage by COMMIT

(?s)(\bRAISERROR\b.*\bCOMMIT\b|\bCOMMIT\b.*\bRAISERROR\b)

Explanation of the regexp; (?s) is the flag for the rather misnamed "single line" mode, it forces . to match newlines. Then find the word "RAISEERROR" followed by an arbitrary number of arbitrary characters followed by "COMMIT". Or the same but in opposite order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM