I had to take over a c# project. The guy who developed the software in the first place was deeply in love with #region
because he wrapped everything with regions.
It makes me almost crazy and I was looking for a tool or addon to remove all #region
from the project. Is there something around?

- 27,557
- 22
- 88
- 134
-
1Are regions a bad thing, or are you talking excessive usage? On a previous project we were taught to setup regions for events, constructors, properties, public methods, private methods, private variables for each class. – JonWillis Feb 22 '11 at 20:36
-
14I guess it's a matter of taste. I just don't see any benefit in using them. For me it's disturbing not seeing the code or to click on the region to open it. – gsharp Feb 22 '11 at 20:44
-
6@JonWillis -- Overall I don't think regions are bad but they have a tendency of hiding violations of the Single Responsibility Principle. – Austin Salonen Feb 22 '11 at 20:47
-
@Austin, that is a fact i never considered. I am not a avoid supporter of them but have no distaste to using them. Maybe regions are best suited to large classes which can't be made smaller for one reason for another. As i would agree on smaller classes it is overkill and enforcing a policy we had wasn't always easy. It was not uncommon for methods to be in the wrong region or out of regions all together, which adds to code maintainece to comply with policies. – JonWillis Feb 22 '11 at 21:20
-
1@AndyM -- Where did I state they should be removed or avoided? – Austin Salonen Jul 21 '11 at 14:19
-
@Austin -- They can make violations of the SRP explicit and visible. Thereby serving as an indicator or prestage for possible future refactoring. Removing all of these (as suggested in the question) would mean losing all this useful info. – – AndyM Jul 21 '11 at 21:12
-
@AndyM -- I assume we're on the same line of thinking but thinking of different users. For a greenfield developer, they _may_ tend to hide violations of SRP and make the rest of the code appear "good." For a brownfield developer, those regions would highlight places to fix (where the greenfield dev got lazy). Now I never said he should remove them and I think just blindly removing all of them is dangerous and flat out irresponsible. – Austin Salonen Jul 21 '11 at 21:48
8 Answers
Just use Visual Studio's built-in "Find and Replace" (or "Replace in Files", which you can open by pressing Ctrl + Shift + H).
To remove #region
, you'll need to enable Regular Expression matching; in the "Replace In Files" dialog, check "Use: Regular Expressions". Then, use the following pattern: "\#region .*\n
", replacing matches with ""
(the empty string).
To remove #endregion
, do the same, but use "\#endregion .*\n
" as your pattern. Regular Expressions might be overkill for #endregion
, but it wouldn't hurt (in case the previous developer ever left comments on the same line as an #endregion
or something).
Note: Others have posted patterns that should work for you as well, they're slightly different than mine but you get the general idea.

- 110,061
- 20
- 134
- 146
-
-
Use appropriate [regexp](http://msdn.microsoft.com/en-us/library/2k3te2cs%28v=VS.100%29.aspx) or [wildcards](http://msdn.microsoft.com/en-us/library/afy96z92%28v=VS.100%29.aspx). – Vlad Feb 22 '11 at 20:21
-
2that's almost embarassing. i never used wildcard/regex search in 10 years of visual studio. :-p – gsharp Feb 22 '11 at 20:28
-
2Might want to add `\n` at the end of the regex so that you’re not left with excessively many blank lines. – Timwi Feb 22 '11 at 20:28
-
2
-
Thank you, this is so brilliant. First time I have used regex search/replace :) – Matěj Zábský Oct 18 '12 at 07:53
-
Please use a Find in files first before you execute an automated remove.. I had to add this to the 'File types' box: !*.designer.vb – JDC Dec 01 '21 at 08:32
Use one regex ^[ \t]*\#[ \t]*(region|endregion).*\n
to find both: region and endregion. After replacing by empty string, the whole line with leading spaces will be removed.
[ \t]*
- finds leading spaces
\#[ \t]*(region|endregion)
- finds #region or #endregion (and also very rare case with spaces after #)
.*\n
- finds everything after #region or #endregion (but in the same line)
EDIT: Answer changed to be compatible with old Visual Studio regex syntax. Was: (question marks do not work for old syntax)^[ \t]*\#(end)?region.*\n
EDIT 2: Added [ \t]*
after # to handle very rare case found by @Volkirith

- 2,413
- 2
- 21
- 26
-
2You sir, are a life saver! I had to inherit a project were every method was wrapped in a region. Every single method! – Draco Sep 11 '13 at 08:17
-
1I added another [ \t]* between # and (region) -> appearantly its also valid to have spaces there and the dev I inherited the code from that I had to cleanup was pretty inconsistent. The full expression I used: ^[ \t]*\#[ \t]*(region|endregion).*\n – Arno Peters Jan 22 '16 at 13:31
-
-
The regex on your first paragraph did the trick for me. Thank you very much. – SoftDev30_15 Jun 16 '20 at 07:57
Should you have to cooperate with region lovers (and keep regions untouched ), then I would recommend "I hate #Regions" Visual Studio extension. It makes regions tolerable - all regions are expanded by default and #region directives are rendered with very small font.

- 2,594
- 18
- 16
For anyone using ReSharper it's just a simple Atr-Enter on the region line. You will then have the option to remove regions in file, in project, or in solution.

- 4,393
- 4
- 27
- 36
-
awesome! R# has now so many cool features that I've lost track of all what's possible. – gsharp Jun 03 '16 at 10:34
In Find and Replace use {[#]<region[^]*}
for Find what:
and replace it with empty string.
#EndRegion
is simple enough to replace.

- 107,317
- 23
- 199
- 210
-
-
@Timwi [^...] `Matches any character that is not in the set of characters that follows the ^`. So in this case the set is empty so matches everything in that line. – Bala R Feb 22 '11 at 20:39
-
To remove #region with a newline after it, replace following with empty string:
^(?([^\r\n])\s)*\#region\ ([^\r\n])*\r?\n(?([^\r\n])\s)*\r?\n
To replace #endregion with a leading empty line, replace following with an empty string:
^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\#endregion([^\r\n])*\r?\n

- 3,009
- 4
- 37
- 51
How about writing your own program for it, to replace regions with nothing in all *.cs files in basePath recursively ?
(Hint: Careful with reading files as UTF8 if they aren't.)
public static void StripRegions(string fileName, System.Text.RegularExpressions.Regex re)
{
string input = System.IO.File.ReadAllText(fileName, System.Text.Encoding.UTF8);
string output = re.Replace(input, "");
System.IO.File.WriteAllText(fileName, output, System.Text.Encoding.UTF8);
}
public static void StripRegions(string basePath)
{
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"(^[ \t]*\#[ \t]*(region|endregion).*)(\r)?\n", System.Text.RegularExpressions.RegexOptions.Multiline);
foreach (string file in System.IO.Directory.GetFiles(basePath, "*.cs", System.IO.SearchOption.AllDirectories))
{
StripRegions(file, re);
}
}
Usage:
StripRegions(@"C:\sources\TestProject")

- 78,642
- 66
- 377
- 442
You can use the wildcard find/replace:
*\#region *
*\#endregion
And replace with no value. (Note the #
needs to be escaped, as visual stuido uses it to match "any number")

- 100,477
- 16
- 156
- 200
-
1@downvoters: How is this any different that the accepted answer? – Brad Christie Jul 06 '15 at 15:14
-
You have started receiving down-votes probably because "wildcard" option was removed in VS2012. VS2010 had this option — compare https://msdn.microsoft.com/en-us/library/a40ywt0a(v=vs.100).aspx and https://msdn.microsoft.com/en-us/library/a40ywt0a(v=vs.110).aspx). It is not fair. – CoperNick Dec 17 '15 at 07:31