Search code examples
regexvisual-studioasync-awaitconfigureawait

Regex to find missing ConfigureAwait


I have a specific pattern that I want to search for in Visual Studio.

Basically, I want to search for lines that contains await, but are missing ConfigureAwait at the end of the statement.

I have some patterns that works in regex-testers such as regex101.com, but I can't get these patterns to work in Visual Studio search. For example, the pattern (?s)^(.)*(await)((?!ConfigureAwait).)*(.)?(;).

What I am doing wrong?

Edit - I want to find lines in my project such as

await DoSomeCoolThings(x, y); (i.e. missing the ConfigureAwait(...).)

but I don't want to get a match for lines such as:

await DoSomeCoolThings(x, y).ConfigureAwait(false);


Solution

  • If the order of await and ConfigureAwait does not matter, then you can use

    (?=.*\bawait\b)(?!.*\bConfigureAwait\b)
    

    otherwise, if you consider that ConfigureAwait should come after await, you can use

    (?=.*\bawait\b(?!.*\bConfigureAwait\b))
    

    Efficient Solution

    (?=\bawait\b(?!.*\bConfigureAwait\b))