[英]Matching last occurance of character using Regex
我需要匹配:
<p><span style="font-size: 18px;"><strong>Hello</strong></span></p>
我需要在最后一個>
和第一個</
之間匹配文本問好
使用(?=>)(.*?)(?=</)
返回<span style="font-size: 18px;"><strong>Hello
謝謝!
我知道這不是您要找的答案,但是用regex解析html就像用叉子吃湯。 您最終會完成工作,但這非常令人沮喪。
嘗試以下方法,保持理智:
string html = "<p><span style=\"font-size: 18px;\"><strong>Hello</strong></span></p>";
System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(html);
string hello = doc.Descendants().LastOrDefault().Value;
你可以去
/>([^<>]+)</
那應該給您想要的比賽。
您只需要匹配此特定字符串? 如果是,那么您可以簡單地使用:
/<strong>([^<]*)</strong>/
它將匹配strong
標簽之間的任何文本。
嘗試這個
正則表達式的常數為
const string HTML_TAG_PATTERN = "<.*?>";
功能
static string StripHTML(string inputString)
{
return Regex.Replace
(inputString, HTML_TAG_PATTERN, string.Empty);
}
然后像這樣調用函數
string str = "<p><span style='font-size: 18px;'><strong>Hello</strong></span></p>";
str = StripHTML(str);
我認為您的look ahead
必須更像: (?<=>)
( look behind
>
)
並替換.*?
通過[^<>]*
(除<
或>
任何字符)。
如果您需要look around
可以執行以下操作: (?<=>)([^<>]*)(?=</)
如果沒有,您可以簡單地做: >([^<>]*)</
不同之處在於,使用look around
您不會在全局匹配中捕獲<
都不</
div </
strong </
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.