1

Lets say I am using Visual Studio's Find and Replace tool and I want to find and comment out every instance of Console.WriteLine(...). However, I can wind up with situations where Console.WriteLine(...) goes across multiple lines like so:

Console.WriteLine("Adding drive to VM with ID: {0}. Drive HostVMID is {1}",
     vm.ID, drive.HostVmId);

These can go on for 2, 3, 4, etc lines and finally end with ); to close the statement. Then I can have other lines that are immediately followed by important blocks of code:

Console.WriteLine("Creating snapshot for VM: {0} {1}", dbVm.ID, dbVm.VmName);
dbContext.Add(new RTVirtualMachineSnapshot(dbVm));

So what I want to do is come up with a regex statement that will find both the first type of instances of Console.WriteLine as well as simple single-line instances of it.

The Regex that I got from another user was:

Console\.writeline(?>.+)(?<!;)

Which will match any line that contains Console.WriteLine but does not end with a semicolon. However I need it to continue on until it finally does reach a closing parenthesis followed by a semicolon.

Ive tried the following regex:

(Console\.writeline(?>.+)(?<!\);)

However I think thats incorrect because it still only matches the first line and doesnt capture the following lines when the writeline spans multiple lines.

At the end of the day I want to be able to capture a full Console.writeline statement regardless of how many lines it spans using Visual Studio's find and replace feature and I am a little confused on the regex I would need to use to do this.

user2357446
  • 656
  • 6
  • 25
  • 1
    Why did you post a new question? I could update my answer at the [previous one](http://stackoverflow.com/questions/34185524/net-regex-negative-lookahead-when-looking-for-semicolons/34185663?noredirect=1#comment56117740_34185663). – Wiktor Stribiżew Dec 09 '15 at 20:52
  • So, what do we do? Close this one as a duplicate or moving the update here? – Wiktor Stribiżew Dec 09 '15 at 21:12
  • Which VS are you using ? Greater or equal to 12 ? –  Dec 09 '15 at 21:14
  • In general, you're not going to be able to parse _all_ the parameters that can be passed to functions. If it's a one-off thing, use the best regex you can and manually find next until you find one you want to replace, then hit replace. –  Dec 09 '15 at 21:18
  • Possible duplicate of [.NET Regex negative lookahead when looking for semicolons](https://stackoverflow.com/questions/34185524/net-regex-negative-lookahead-when-looking-for-semicolons) – Armali Sep 22 '17 at 06:51

1 Answers1

0

I guess you could try this which just looks for a pseudo termination,
but does not take into account string quotes.

(?s)\b(Console\s*\.\s*WriteLine\s*\((?:(?!\)\s*;).)*\)\s*;)

Formatted:

 (?s)
 \b
 (                             # (1 start)
      Console \s* \. \s* WriteLine \s* \(
      (?:
           (?! \) \s* ; )
           . 
      )*
      \) \s* ;
 )                             # (1 end)

add:

If you cannot use dot-all modifier (?s) and Dot-all options are not available (should be),
substitute [\S\s] for the dot.

Then just substitute "/** $1 **/" to comment it out.