replacing x number of blank lines with two blank lines

Find, replace, find in files, replace in files, regular expressions

replacing x number of blank lines with two blank lines

Postby bobcdy » Mon Oct 25, 2010 11:23 pm

I'm new to regex, and often have a problem with my html documents. I need to relace multiple blank lines with only two blank lines. It's very simple using Word 2003 so I guessed that Perl regex will do at least as well because it's more powerful and flexible. I searched the posts, however, and can't find anything that seems to solve my problem.

Can anyone help me?
bobcdy
Newbie
 
Posts: 6
Joined: Tue Jul 06, 2010 1:02 pm

Re: replacing x number of blank lines with two blank lines

Postby Bracket » Mon Oct 25, 2010 11:38 pm

Easy. Do a Perl RegEx Replace All:

Find Text:

(\r\n){2,}


Replace Text:

\r\n\r\n\r\n


Now, you said "x number of blank lines", you didn't say "anything greater than two blank lines". So, the RegEx I provided will do this where you only have one blank line as well. If this was not your intention, than replace the "2" with a "4", and you'll be set.
User avatar
Bracket
Basic User
Basic User
 
Posts: 32
Joined: Fri Oct 26, 2007 11:00 pm

Re: replacing x number of blank lines with two blank lines

Postby bobcdy » Tue Oct 26, 2010 1:02 am

Bracket,

Wow! Worked perfectly. Thanks much!
Bob
bobcdy
Newbie
 
Posts: 6
Joined: Tue Jul 06, 2010 1:02 pm

Re: replacing x number of blank lines with two blank lines

Postby Bracket » Tue Oct 26, 2010 8:37 am

You're very welcome. :)
User avatar
Bracket
Basic User
Basic User
 
Posts: 32
Joined: Fri Oct 26, 2007 11:00 pm

Re: replacing x number of blank lines with two blank lines

Postby bulgrien » Tue Oct 26, 2010 5:11 pm

bobcdy wrote:I need to relace multiple blank lines with only two blank lines.

FYI - UltraEdit supports multi-line find and replace strings. While regex is the more elegant solution (you can replace any number of blank lines at a time as Bracket demonstrated) you can also perform a non-regex search and replace of blank lines by using Ctrl+J keypresses in the Find What and Replace With fields.
User avatar
bulgrien
Master
Master
 
Posts: 92
Joined: Fri Dec 11, 2009 1:02 am
Location: Pennsylvania, USA


Return to Find/Replace/Regular Expressions