其中包含多个\ r \ n以及何时使用
Regex.Replace(sampleText, @"[\r\n]{2,}", "<br />");
它将多个\ {更改为多个<br>
。
我需要将这些多个<br>
替换为单<br />
Regx之后的文字是:
Addendum 5 changes the following:<div><br></div>div<br>div> Addenda 5 - Landscape
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<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=""> <br>span>
<br>Colo<br>to<br>e dark<br>own.<span style=""> <br>span>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="">
<br>span>'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=""> <br>span>
<br>Install 1 1/2" depth mulch
<br>@ 42 sf.<span style=""> <br>span>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=""> <br>span>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=""> <br>span>
<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=""> <br>span>
<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=""> <br>span>
<br>-7
<br>Cups
<br>
<br>
<br>
<br>Deduct
<br>20-10-5<span style=""> <br>span>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 <br>div<br>div<br>div></span></span></span></span></span></span></span></span></span></span>
请帮忙, 感谢。
答案 0 :(得分:7)
也许是这样的:
Regex.Replace(sampleText, @"[(<br( /|)>|<br( /|)>\n)]{2,}", "<br />");
应检查<br>
或<br />
是否有\n
个换行符。
我没有对它进行过测试,如果<br>
和换行符之间有空格可能会出现问题
答案 1 :(得分:4)
以下正则表达式应该有效:
/(<br>\s*){2,}/
它基本匹配一对<br>
之间的2个或更多个连续的<br>
和可选的空格字符。然后,replace
方法可用于仅使用一个<br>
替换匹配的元素。
可在以下位置获取演示:http://jsfiddle.net/DA9h4/。它使用javascript,但同样的正则表达式也适用于其他语言。
var str = "Addendum 5 changes the following:<div><br></div>div<br>div> Addenda 5 - Landscape<br><br><br><br><br><br> <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 :(得分: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
答案 3 :(得分:1)
尝试使用以下表达式添加<br />
代码:
Regex.Replace(sampleText, @"\r?\n\s*", "<br />");
答案 4 :(得分:1)
C#
Regex.Replace(sampleText, @"(<br ?/?>)+", "<br />");
的jQuery /普通的老的JavaScript
sampleText.replace(/(<br ?\/?>)+/, "<br />");
希望这有帮助
答案 5 :(得分:1)
关于使用一行中的顺序匹配字符串或其间有空格的多行的这类正则表达式的一个重要事项是考虑任何空白字符[\r\n\t\f ]
,所以我们应该去写\s
为了我们的目的,因为它包括上面的所有这些字符。
((<br>\s*)\1?)+
这个正则表达式的一个奇怪的部分是使用反向引用\1
它将检查最近捕获的每个相同的组。
所以在你的情况下你将要使用:
Regex.Replace(sampleText, @"((<br>\s*)\1?)+", "<br />");
您可以看到live demo here
答案 6 :(得分:1)
这是正常的,请尝试一次。
Regex.Replace(sampleText,@“(
\ s *)\ 1 +”,“
”);
答案 7 :(得分:0)
您想删除额外的<br>
,直到只剩下一个:
do
{
yourText.Replace("<br><br>","<br>")
} while (yourText.Contains("<br><br>"))