4

Which contains multiple \r\n and when I used

Regex.Replace(sampleText, @"[\r\n]{2,}", "<br />");

it changes it to with multiple <br> for multiple \n. I need to replace these multiple <br> with single <br />

the text after Regx is:

Addendum 5 changes the following:<div><br></div>div<br>div&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Addenda 5 - Landscape
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>&nbsp;
<br>Item
<br>Description
<br>Qty
<br>Unit
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>Add
<br>Pavestone Hollandstone Pavers<span style="">&nbsp;<br>span&gt;
<br>Colo<br>to<br>e dark<br>own.<span style="">&nbsp;<br>span&gt;Base material not specified. Pavers to<br>e
<br>ADA compliant.
<br>478
<br>SF
<br>
<br>
<br>
<br>Area w<br>Calgary
<br>Carpet Junipe<br>is clouded on sheet L5.1. Unable to determine what changed in
<br>that area.
<br>
<br>
<br>
<br>Deduct
<br>Prunus laurocerasus 'Otto Luykens', 5 gal
<br>Otto Luykens Laurel
<br>-1
<br>EA
<br>
<br>
<br>
<br>Deduct
<br>Miscanthus sinensis<span style="">&nbsp;
<br>span&gt;'Morning Light', 5 gal
<br>Morning Light Miscanthus
<br>-3
<br>EA
<br>
<br>
<br>
<br>Add
<br>Rose 'Knockout', 5 gal
<br>Rose Knockout
<br>11
<br>EA
<br>
<br>
<br>
<br>Deduct
<br>Turf-N Tee Mulch<span style="">&nbsp;<br>span&gt;
<br>Install 1 1/2" depth mulch
<br>@ 42 sf.<span style="">&nbsp;<br>span&gt;50% fo<br>each mulch type
<br>-0.1
<br>CY
<br>
<br>
<br>
<br>Deduct
<br>Wood Shavings Mulch
<br>Install 1 1/2" depth mulch
<br>@ 42 sf.<span style="">&nbsp;<br>span&gt;50% fo<br>each mulch type
<br>-0.1
<br>CY
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>Deduct
<br>12-8-8 Gro-Power
<br>Mix into planting areas at 75
<br>lbs/1000 sf @ 42 sf pe<br>spec 329500-3.6.
<br>-3
<br>LBS
<br>
<br>
<br>
<br>Deduct
<br>5-3-1 Gro-Powe<br>Plus
<br>Mix into planting areas at 150
<br>lbs/1000 sf @ 42 sf pe<br>spec 329500-3.6.
<br>-6
<br>LBS
<br>
<br>
<br>
<br>Deduct
<br>Flou<br>of Sulfur
<br>Mix into planting areas at 20
<br>lbs/1000 sf @ 42 sf pe<br>spec 329500-3.6.
<br>-1
<br>LBS
<br>
<br>
<br>
<br>Deduct
<br>Gro-Safe Activated Charcoal
<br>Mix into planting areas at 200
<br>lbs/1000 sf @ 42 sf pe<br>spec 329500-3.6.
<br>-8
<br>LBS
<br>
<br>
<br>
<br>Deduct
<br>Plant Backfill: Compost
<br>Apply 20% compost mix and 80%
<br>native soil pe<br>detail B/L5.6.
<br>-1
<br>LS
<br>
<br>
<br>
<br>Deduct
<br>Plant Backfill: Nitrogenized
<br>Stabilized Organic Amendment
<br>Fo<br>all container-sized plants,
<br>ackfill shall<br>e a uniform mixture of the following ingredients<br>y volume):
<br>6 parts<br>y volume on-site soil, 4 parts<br>y volume nitrogen stabilized organic
<br>amendment, Grow powe<br>pe<br>manufacturers'<br>ecommendation, 2 lbs. iron sulfate
<br>pe<br>cubic yard of mix pe<br>spec 329500-3.6.
<br>-1
<br>LS
<br>
<br>
<br>
<br>Deduct
<br>Plant Backfill: 5-3-1 Gro-Power
<br>Plant Backfill: 7-5 gal @ 1 cup
<br>pe<br>specs 329500-3.6.<span style="">&nbsp;<br>span&gt;
<br>-7
<br>Cups
<br>
<br>
<br>
<br>Deduct
<br>Plant Backfill: Flou<br>of Sulfate
<br>Plant 7-5 gal @ 1/2 cup per
<br>specs 329500-3.6.<span style="">&nbsp;<br>span&gt;
<br>-3.5
<br>Cups
<br>
<br>
<br>
<br>Deduct
<br>Plant Backfill: Gro-Safe
<br>Plant Backfill:7-5 gal @ 1 cup
<br>pe<br>specs 329500-3.6.<span style="">&nbsp;<br>span&gt;
<br>-7
<br>Cups
<br>
<br>
<br>
<br>Deduct
<br>20-10-5<span style="">&nbsp;<br>span&gt;Plante<br>Tablets
<br>- 21 gram
<br>Apply to planting pits at:7-5
<br>gal @ 2 tablets pe<br>specs 329500-3.6.
<br>-14.00
<br>EA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>div<br>div<br>div&gt;</span></span></span></span></span></span></span></span></span></span>

Please help, Thanks.

LOZ
  • 1,169
  • 2
  • 16
  • 43
Anil D
  • 41
  • 3
  • I see no reason why the regex you're using right now wouldn't already do this, unless there is whitespace on the blank lines (but there isn't in what you pasted in). Ahh I see I've been tricked by a bounty on a super old question..... – OGHaza Dec 19 '13 at 10:19
  • 1
    That text is seriously messed up. `
    Fo
    all container-sized plants,` I guess should be `
    For all container-sized plants,` and there's more like that...
    – Jerry Dec 19 '13 at 17:38
  • Zalgo is the pony... http://stackoverflow.com/a/1732454/945456 – Jeff B Dec 20 '13 at 19:32

8 Answers8

7

Maybe something like this:

Regex.Replace(sampleText, @"[(<br( /|)>|<br( /|)>\n)]{2,}", "<br />");

should check for <br> or <br /> with possible \n line breaks.

I have not tested it and there might be some problems if there are space between the <br> an the line-breaks

Pit
  • 1,448
  • 1
  • 18
  • 26
4

The following regular expression should work:

/(<br>\s*){2,}/

It basically matches 2 or more consecutive <br>'s and optional white space characters between a pair of <br>'s. Then the replace method can be used to replace the matched element with just one <br>.

A demo is available at: http://jsfiddle.net/DA9h4/. It uses javascript but the same regex is applicable in other languages too.

var str = "Addendum 5 changes the following:<div><br></div>div<br>div&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Addenda 5 - Landscape<br><br><br><br><br><br>&nbsp;<br>Item";

var r = new RegExp("(<br>\\s*){2,}");

console.log("old value: " + str);
console.log("new value: " + str.replace(r, "<br>"));

As for the original question which uses \r\n, the following similar regex would work:

/(\r\n\s*){2,}/
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
2

You can use this:

Regex.Replace(sampleText, @"[^\S\n]*\n\s*", "<br />");

The pattern will find each group of white characters that contains a newline and replace it with a single <br />.

[^\S\n]*    # all white characters except \n, zero or more times
\n          # a newline character
\s*         # all white characters, zero or more times

The first part doesn't contain newlines and is used to trim other leading white-characters ( including the \r) until the first newline character.

The last part matches all white-characters including possible other newlines or carriage returns.

Note that the newline type (CRLF, LF) of your string doesn't matter with this pattern, since it only looks for \n

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

Try using the following expression for adding your <br /> tags:

Regex.Replace(sampleText, @"\r?\n\s*", "<br />");
JonM
  • 1,314
  • 10
  • 14
1

C#

Regex.Replace(sampleText, @"(<br ?/?>)+", "<br />");

jQuery/regular-old-javascript

sampleText.replace(/(<br ?\/?>)+/, "<br />");

Hope this helps

loushou
  • 1,462
  • 9
  • 15
1

One important thing about these kind of Regular Expressions using sequential match strings in one line or multi-lines with spaces between, is to consider any white space character [\r\n\t\f ] so we should go to write \s for our purpose since it includes all of those characters above.

((<br>\s*)\1?)+

The may be odd part of this regex is the use of back refrence \1 it will check for each same group captured lately.

so in your case you're going to use:

Regex.Replace(sampleText, @"((<br>\s*)\1?)+", "<br />");

which you can see its live demo here

revo
  • 47,783
  • 14
  • 74
  • 117
1

This is working correctly, try once.

Regex.Replace(sampleText, @"(
\s*)\1+", "
");

Neeraj Sharma
  • 346
  • 1
  • 4
  • 16
0

You want to remove extra <br> until only one left so:

do
  {
      yourText.Replace("<br><br>","<br>")
  } while (yourText.Contains("<br><br>"))
Kuzgun
  • 4,649
  • 4
  • 34
  • 48