2010-12-13 44 views
4

一個包含多個\ r \ n替換多個
成單
並且當我用使用C#或Jquery的

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

它它改變到具有多個<br>用於多個\ n。 我需要一個<br />

替換這些多<br>文本至REGx是後:

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> 

請幫幫忙,謝謝 。

+0

我看不出有什麼理由你現在正在使用正則表達式不會已經做到這一點,除非有空白的空白行(但是粘貼的內容不存在)。啊,我看到我被一個超級老問題的賞金騙了..... – OGHaza 2013-12-19 10:19:42

+1

這段文字被嚴重搞砸了。 '

所有的容器大小的植物,'我想應該是'
對於所有的容器大小的植物,'還有更多類似的... – Jerry 2013-12-19 17:38:05

+0

Zalgo是小馬... http://stackoverflow.com/a/1732454/945456 – 2013-12-20 19:32:21

回答

7

也許是這樣的:

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

應該檢查<br><br />有可能\n換行符。

我沒有測試過,如果有在<br>之間空間的換行符可能會有一些問題

1

嘗試使用下面的表達式添加您的<br />標籤:

Regex.Replace(sampleText, @"\r?\n\s*", "<br />"); 
4

的下面的正則表達式應該工作:

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

它基本上匹配2個或更多的連續<br>的和一對<br>之間的可選空白字符。然後可以使用replace方法用一個<br>替換匹配元素。

演示可在:http://jsfiddle.net/DA9h4/。它使用JavaScript,但同樣的正則表達式也適用於其他語言。

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>")); 

至於它採用\r\n原來的問題,以下類似的正則表達式將工作:

/(\r\n\s*){2,}/ 
2

您可以使用此:

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

模式會發現每一組的白包含換行符的字符並將其替換爲單個<br />

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

第一部分不包含新行和用於微調其他領先白字符(包括\r直到第一換行符。

最後一部分匹配所有白色字符,包括可能的其他換行符或回車符。

請注意,您的字符串的換行符類型(CRLF,LF)不使用此模式沒關係,因爲它只查找\n

1

C#

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

的jQuery /常規舊的javascript

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

希望這有助於

1

一個約個重要的事情使用順序匹配字符串在一行或多行之間使用空格的正則表達式是考慮任何空格字符[\r\n\t\f ],所以我們應該去寫\s爲我們的目的,因爲它包括上述所有這些字符。

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

的可以是此正則表達式的奇數部分是使用背面refrence \1的它將檢查最近捕獲的每個相同的基團。

所以你的情況,你要使用:

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

,你可以看到它的live demo here

1

這是正常工作,嘗試一次。 Regex.Replace(sampleText,@「(
\ s *)\ 1+」,「
」);

0

您要刪除額外<br>,直到只剩下一個這樣:

do 
    { 
     yourText.Replace("<br><br>","<br>") 
    } while (yourText.Contains("<br><br>")) 
+0

事情非常糟糕。考慮'(
)+'作爲搜索條件 – Jan 2013-12-26 09:28:31