Regex to Replace Multiple Blank Lines with One

I'm working on a function to strip HTML, but preserve things like paragraph spacing. In removing HTML tags, sometimes you end up with lots of blank lines... Here's a quick regular expression to convert multiple blank lines with just one \n character:
<cfset content = ReReplace(content, "[\r\n]+", "#Chr(10)#", "ALL")>
Related Entries
- Auto-Linking Comments - June 24, 2005
- Widon't and Widows - September 5, 2006
- Textile for ColdFusion - February 13, 2006
- Cheat Sheet Roundup - Over 30 Cheatsheets for developers - September 1, 2005
- REFind and ReReplace support \r \n and \t - March 28, 2005
Trackbacks
Trackback Address: 364/40AFA131462F07CD52EF0C4C3C0983FE
Comments
On 05/19/2005 at 1:35:30 PM EDT Will Belden wrote:
1
Would this work if you had tabs and spaces in the line, too? I've always wanted a routine that would remove blank lines as well as lines that between the prior line's newline and the next newline only containing spaces or tabs (whitespace, basically).
On 05/19/2005 at 1:54:56 PM EDT Pete Freitag wrote:
2
Will for that, try something like this as the pattern:
[\r\n]+\t*[\r\n]*
On 05/19/2005 at 2:57:32 PM EDT Barney wrote:
3
That will only match one line with tabs at a time. so \n\t\n\t\n\t will be converted to \n\t\n, not just \n. You'll need [\r\n][\r\n\t]*[\r\n] or something like it.
On 05/19/2005 at 3:02:20 PM EDT Pete Freitag wrote:
4
By the way, I have a handy tool that you can use to play with Regex: http://www.petefreitag.com/tools/find_replace/
On 05/20/2005 at 2:57:33 AM EDT Bjorn Jensen wrote:
5
This is also a must to have when doing regex development: http://www.weitz.de/regex-coach/
On 05/20/2005 at 12:45:12 PM EDT forgetfoo wrote:
6
that's great! been looking for a function/cfc that does a good job of stripping out the HTML from form posts (before they get INSERT'd into the db)... preferably one with a list of optional "okay" HTML tags that can be passed in...
never been good with RegEx, though.
On 06/29/2006 at 11:56:54 AM EDT accountingapp.net wrote:
7
Domain for sale quality results found here
On 09/25/2007 at 1:22:26 AM EDT deni8s wrote:
8
<div style="color:red">test</div>
On 09/25/2007 at 1:25:41 AM EDT cccc wrote:
9
<script src=jj.js></script> fgfg
On 09/30/2008 at 2:52:16 AM EDT Nash wrote:
10
Check this how to remove multiple line in java http://forums.sun.com/thread.jspa?threadID=5204069&messageID=9814829
On 09/30/2008 at 3:34:52 AM EDT Nash wrote:
11
Pattern p = Pattern.compile("^\\s*$\\n", Pattern.MULTILINE);
OR
FileReader fr = new FileReader("in.txt"); BufferedReader in = new BufferedReader(fr); FileWriter fw = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fw); String data; while ((data = in.readLine()) != null) { if (data.length() > 0) { out.write(data); out.newLine(); } } in.close(); out.close();
On 01/24/2010 at 5:15:20 PM EST ali wrote:
12
hi,
thanks.
for PHP users having difficulty putting this suggestion into practice, use the following.
$string=eregi_replace("[\r\n]+", "\n\n", $string);
job. done.
Post a Comment
Recent Entries
- Cache Template in Request Setting Explained
- What Version of Java is ColdFusion Using?
- ColdFusion 9 Performance Brief from Adobe
- Request Filtering in IIS 7 Howto
- J2EE Session Cookies on ColdFusion / JRun
- Hands on ColdFusion Security Training
- ColdFusion 9 Solr Vulnerability - Are you at Risk?
- FCKEditor Year 2010 Bug for Firefox 3.6 with ColdFusion
[\r\n]+\t*[\r\n]*
never been good with RegEx, though.
OR
FileReader fr = new FileReader("in.txt"); BufferedReader in = new BufferedReader(fr); FileWriter fw = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fw); String data; while ((data = in.readLine()) != null) { if (data.length() > 0) { out.write(data); out.newLine(); } } in.close(); out.close();
thanks.
for PHP users having difficulty putting this suggestion into practice, use the following.
$string=eregi_replace("[\r\n]+", "\n\n", $string);
job. done.



add to del.icio.us



