Friday, August 30, 2013

CSharp C# Regular Expression RegExp for SEARCH and REPLACE with examples Notepad++ and Visual Studio

CSharp C# Regular Expression RegExp for SEARCH and REPLACE with examples Notepad++ and Visual Studio




Base CLK example using Notepad++ (note that it will not work if file is not saved...):


Console.WriteLine("Stuff n more stuff");
Search:  (Console.?WriteLine\()(\").*(\"\);)
replace: \1\2dog\3



Example:  Find Console.WriteLine items with "," and {} references.  Replace Console.WriteLine with LogInforAndConsole and surround full text, comma, and value in string or variable with the String.Format function.  And ignore non-comma signatures.
Before:
Console.WriteLine("Stuff n more stuff={0}", "something else");
Console.WriteLine("Hello all!");
Console.WriteLine("Stuff n more stuff={0}", myVar);


Search:  (Console.?WriteLine\()(\")(.*,.*)(\);)
                       \1                      \2   \3  \4
replace: LogInfoAndConsole\(string.Format\(\2\3\)\4
replace: LogInfoAndConsole(string.Format($2$3)$4    ($ instead of \# and no slash(escape) in front of parenthesis with C# and Visual Studio search)


After:
LogInfoAndConsole(string.Format("Stuff n more stuff={0}", "something else"));
Console.WriteLine("Hello all!");
LogInfoAndConsole(string.Format("Stuff n more stuff={0}", myVar));


Example:  Find all simple Console.Writeline items and replace Console.WriteLine with LogInforAndConsole.
Before:
 Console.WriteLine("Hello all!");


Search:  (Console.?WriteLine\()(\")(.*)(\);)
                       \1                      \2   \3  \4
replace: LogInfoAndConsole\(\2\3\4
replace: LogInfoAndConsole(s$2$3$4    ($ instead of \# and no slash(escape) in front of parenthesis with C# and Visual Studio search)


After:
LogInfoAndConsole("Hello all!");


Example: Find all Console.WriteLine with variable only values and substitute Console.WriteLine with LogInfoAndConsole.
Before:
Console.WriteLine(verificationErrors.ToString());
Console.WriteLine(myVar);


Search:  (Console.?WriteLine\()(.*)(\);)
                       \1                        \2   \3
replace: LogInfoAndConsole\(\2\3
replace: LogInfoAndConsole($2$3    ($ instead of \# and no slash(escape) in front of parenthesis with C# and Visual Studio search)


After:
LogInfoAndConsole(verificationErrors.ToString());
LogInfoAndConsole(myVar);

Example of Notepad++ UI:






No comments:

Post a Comment