Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

Visual Studio has ability to delete empty lines in replace operation using regular expressions.
1.Click Ctrl-H (quick replace)
2. Tick "Use Regular Expressions"
3. In Find specify ^$\n
4. In Replace box delete everything.
5 Click "Replace All"
All empty lines will be deleted.

 Regular expression for empty line consist of   

Beginning of line ^

 

 

End of line $

 

Line break \n

 

Note that normally in Windows an end of line  indicated by 2 characters CRLF - Carriage Return (CR, ASCII 13, \r) Line Feed (LF, ASCII 10, \n).

Addition from  Kenneth at 6/18/2009:
A regex to remove blank lines that are/aren't *really* blank (i.e. they do/don't have spaces):   ^:b*$\n

Addition from   Igor  at 8/5/2011:

To remove double lines:  ^:b*\n:b*\n replace with:  \n

 

To delete  rows with single comma

3. In Find specify ^,$\n

 

For similar requirement to remove comma at the end of lines in text, do the following steps.


1.Click Ctrl-H (quick replace)
2. Tick "Use Regular Expressions"
3.
 In Find specify ,\r?\n.
4. In Replace box type \n

5 Click "Replace All"

 

 

Posted on Tuesday, March 4, 2008 4:28 PM | Back to top


Comments on this post: Remove empty lines in text using Visual Studio.

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Cool, thanks!
Left by Rob on May 15, 2008 4:12 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Oh man i've been asking myself how to do this for years :D
thanks for the neat tip! this helped me a lot!!
Left by felipe on May 30, 2008 1:18 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thanks alot :)

VS added 350.000 empty lines in one of my files..
Left by Asbjorn on Jul 31, 2008 3:37 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
thanks! could you explain the regex? why is the newline character after the end of line marker?
Left by Pete on Sep 06, 2008 4:34 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Pete,
I've added more info about regEx into my post.
Left by Michael Freidgeim on Sep 06, 2008 2:42 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
thanks for the tips.
after playing with it, i find \t\n with wildcards configuration do the works too.
Left by Sung on Oct 02, 2008 9:53 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
This doesn't work in Visual Studio 6.0 (As far as I could tell.) Yes we are stuck in the dark ages.
I suppose I could download Emacs... and be done with this problem.
Left by Ross Youngblood on Oct 20, 2008 8:50 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Ross,
You also can download Visual Studio 2008 Express, which is a good editor (even if you have to compile in Visual Studio 6.0)
Left by Michael Freidgeim on Oct 20, 2008 10:06 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
cool! tips :D
Left by shophola on Apr 27, 2009 3:45 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thanks really this code is a life saver
Left by Gyanendra Kumar Mishra on May 18, 2009 2:47 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
I would offer an addendum:

A regex to remove blank lines that are/aren't *really* blank (i.e. they do/don't have spaces):

^:b*$\n
Left by Kenneth on Jun 18, 2009 3:28 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
simple when you see the answer but I wasted and hour or so trying combinations until I found this solution. Well done.
Left by Steven Chalk on Aug 23, 2009 6:18 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
well. it is a lifesaver. i think the first line should be changed to ctrl+H instead of ctrl+D as mentioned
Left by samy on Dec 01, 2009 11:49 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
samy, thanks, it's indeed ctrl-H
Left by Michael Freidgeim on Dec 02, 2009 5:44 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Great solution in short.

Thanks,
Haidar
Left by Haidar on Dec 03, 2009 6:55 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thank you. helped me in formatting code which I got it from the net. Worked in a jiffy
Left by Vaishnavi on Dec 22, 2009 2:54 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
thank you so much, i hate those blank lines when ever i paste some code from website or notepad. thanks to your tip i will never see those lines again.
Left by Nouman Zakir on Mar 23, 2010 11:25 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
ultimate..
it workss..!!
really thanks
Left by Sagar on Mar 31, 2010 8:56 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
wow! why i didn't think of that before?!?
I think the 'magic' is in the $\n .
So,from now I will use $\n everytime I want to mass delete certain lines without leaving blank line trace. Thanks!!
Left by mhd on Mar 31, 2010 11:08 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Kenneth, perfect for cleaning up the mess VS sometimes makes of the html.

thanks,

danbo
Left by danbo on May 10, 2010 4:22 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Gracias por la solución (Tanx 4 the solution) Great
Left by ELemus on May 25, 2010 2:20 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thanks!!! 8))
Left by Roman on May 28, 2010 8:09 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thanks for this post. I found the resulting no lines a bit difficult to read.

I used

Search for: ^$\n+
Replace With:\n
Left by Ed on Nov 11, 2010 1:55 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thanks A lot kenneth.
Left by mt on Jan 25, 2011 6:38 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
What about going the other way and inserting carriage returns?

tried replacing with \n or \r

predictably just adds '\n'
Left by Anthony on Jan 31, 2011 3:48 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Just make sure regular expression check box in the options is checked, then it should work
Left by Anthony on Jan 31, 2011 3:51 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
724 occurrence(s) replaced - in 0 seconds!
Dude that is one awesome tip :-D
Left by EvilDr on Feb 09, 2011 2:53 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
works perfectly thanks!
Left by Tim on Jun 30, 2011 10:41 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Great, I modifyed your code to remove double lines:
^:b*\n:b*\n
replace with:
\n
Worked like a charm!
Left by Igor on Aug 05, 2011 11:08 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thank u ...For this post...

Left by Srinu on Aug 11, 2011 11:33 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Thank u ...For this post...
even i've been asking myself how to do this for years :D
Left by logicalweb on Aug 12, 2011 5:23 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
GREAT JOB!!!!
Left by mice from space on Oct 03, 2011 6:05 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
This no longer works in VS 11, Leaving the Replace with field blank just makes it insert an emptly line. Any ideas?
Left by Matthew on Mar 04, 2012 5:59 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
This is very similar to what I am looking for. Can someone please help me out with the following RegEx pattern to find the following code in my JavaScript file.

I'm looking for all of the jQuery $("#some-element-by-id") or $(".some-element-by-class") references that do not contain either a # or . inside them which is breaking my Replace function. I have a 6000 line JavaScript file so its been tough to locate the culprit.

I tried the following with no such luck.
$("[^#]
$("[^.]
$("[^#*]")
$("[^.*]")
Left by Ryan Martin on Nov 17, 2012 9:36 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
Ryan,
You don't need to use regular expression. Just search for $(" as string, (not regular expressions) to output all relevant records to "search results" window.
Then copy output to a new file and in this file replace all occurrences of $(". And $("# with empty strings. Remaining rows with $(" will be what are you looking for.
Left by michael freidgeim on Nov 17, 2012 9:33 PM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
great post, thanks, it set me on the right track

my objective was to remove surplus blank lines within vb code window, i.e multiple blank lines(including tabs and spaces) would all be reduced to one empty line (no spaces and no tabs)

i was able to run the double lines example recursively and it gave me the result that i was after. Or at least i thought it did until i opened my files back up and studio was complaining about inconsistent line endings, i think we ended up with LF when it was looking for CRLF

i propose the following which resolves both of these issues
find ^{:b*\n}+:b*\n
replace \x000D\x000A

^ start of line
{:b*\n}+ one or more lines containing only zero or more whitespace chars
:b*\n exactly one line containing only zero or more whitespace chars

\x000D\x000A unicode identifiers for CR followed by LF

i can now run this against the whole solution and achieve exactly what i want in one step
Left by thehill on Jan 12, 2013 9:56 AM

# re: Remove empty lines in text using Visual Studio.
Requesting Gravatar...
a
Left by a on Jul 24, 2014 10:16 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free