Alois Kraus

blog

  Home  |   Contact  |   Syndication    |   Login
  133 Posts | 8 Stories | 368 Comments | 162 Trackbacks

News



Archives

Post Categories

Programming

I had to remove some file headers in a bigger project which were all of the form

#region File Header
/*[ Compilation unit ---------------------------------------------------------- 
     Name            : Class1.cs 
     Language        : C#
    Creation Date   : 
    Description     :

-----------------------------------------------------------------------------*/
/*] END */
#endregion

I know that would be a cool thing to write a simple C# program use a recursive file search, read all lines skip the first n lines and write the files back to disc. But I wanted to test things first before I ruin my source files with one little typo. There comes the Visual Studio Search and Replace in Files dialog into the game. I can test my regular expression to do a multiline match with the Find button before actually breaking anything. And if something goes wrong I have the Undo button.

 

image

There is a nice blog post from Paulo Morgado online who deals with Multiline Regular expressions. The Visual Studio Regular expressions are non standard so you have to adapt your usual Regex know how to the other patterns. The pattern I cam finally up with is

\#region File Header:b*(.*\n)@\#endregion

The Regular expression can be read as

\#region File Header Match “#region File Header” \# Escapes the # character since it is a quantifier.
:b* After this none or more spaces or tabs can follow (:b stands for space or tab)
(.*\n)@ Match anything across lines in a non greedy way (the @ character makes it non greedy) to prevent matching too much until the #endregion somewhere in our source file.
\#endregion Match everything until “#endregion” is found

I had always knew that Visual Studio can do it but I never bothered to learn the non standard Regex syntax. This is powerful and it is inside Visual Studio since 2005!

posted on Saturday, May 29, 2010 6:53 PM

Feedback

# re: Multiline Replacement With Visual Studio 6/15/2010 8:06 AM TheXenocide
Great tip; I've been using this for quite some time, but I'll say the thing I find lacking is that you can't specify new lines in your replacement value. I haven't tested 2010 for this, but I've had poor experiences with this in the past, I think resorting to capturing a known linefeed and using it in the replacement expression or something.

# re: Multiline Replacement With Visual Studio 4/9/2011 9:18 PM Kirill Sorudeykin
regexpression: {Handler\<}{.*}{\>}{.*:b*(.*\n)@.*:b*hWnd, }{msg}

code:
Handler<WM_FONTCHANGE>
{
protected:
virtual void OnFontChange() = 0;

public:
inline LRESULT OnFontChange(WPARAM wParam, LPARAM lParam)
{
OnFontChange();
return 0L;
}

static void Forward(HWND hWnd, forwardFn_t pfn)
{
(*pfn)(hWnd, msg


replace with: \1\2\3\4\2

result:
Handler<WM_FONTCHANGE>
{
protected:
virtual void OnFontChange() = 0;

public:
inline LRESULT OnFontChange(WPARAM wParam, LPARAM lParam)
{
OnFontChange();
return 0L;
}

static void Forward(HWND hWnd, forwardFn_t pfn)
{
(*pfn)(hWnd, WM_FONTCHANGE

# re: Multiline Replacement With Visual Studio 6/13/2013 9:59 AM UTCD
Thanks! That was not intuitive, but you figured it out.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: