我有一個包含 html 內容的字串,例如:
string myMessage = "Please the website for more information (<a class=\"link\" href=\"http://www.africau.edu/images/default/sample.pdf\" target=_blank\" id=\"urlLink\"> easy details given</a>)";
我最終需要的是:
string myMessage = "Please the website for more information http://www.africau.edu/images/default/sample.pdf easy details given";
我可以這樣做替換每個字串,myMessage = myMessage.Replace("string to replace", "");但是然后我必須接收每個字串并將其替換為空。有沒有更好的解決方案?
uj5u.com熱心網友回復:
如果我理解正確,你有一個更大的文本,其中多次出現“<a ....>”,實際上你只想用href.
不確定這是否會讓你更容易,但你可以使用Regex.Matches類似的東西
var myMessage = "Please the website for more information (<a class=\"link\" href=\"http://www.africau.edu/images/default/sample.pdf\" target=_blank\" id=\"urlLink\"> easy details given</a>)";
var matches = Regex.Matches(myMessage, "(. ?)<a. ?href=\"(. ?)\". ?<\\/a>(. ?)");
var strBuilder = new StringBuilder();
foreach (Match match in matches)
{
var groups = match.Groups;
strBuilder.Append(groups[1]) // Please the website for more information (
.Append(groups[2]) // http://www.africau.edu/images/default/sample.pdf
.Append(groups[3]); // )
}
Debug.Log(strBuilder.ToString());
那么這有什么作用呢?
(. ?)將在第一次遇到以下內容之前為所有內容創建一個組<a=>groups[1](<a. ?href=")匹配<a以href="=>開頭和結尾的所有內容被忽略(. ?)將創建一個組之間的所有內容href="和下一個"(因此URL)=>groups[2](". ?<\/a>)匹配從"直到下一個</a>=> 忽略的所有內容(. ?)將在</a>=> groups[3]之后為所有內容創建一個組
并且groups[0]是整場比賽。
所以最后我們只想結合
groups[1] groups[2] groups[3]
但在回圈中,我們可能會在同一字串中找到多個匹配項,為此使用 a 更有效StringBuilder。
結果
Please the website for more information (http://www.africau.edu/images/default/sample.pdf)
你可以簡單地調整它,例如也洗掉( )或包含標簽之間的文本,但我認為實際上這現在最有意義。
uj5u.com熱心網友回復:
我個人不喜歡依賴字串格式總是我所期望的,因為這可能會導致錯誤。
相反,我提供了兩種我能想到的方法:
使用正則運算式:
string myMessage = "Please the website for more information (<a class=\"link\" href=\"http://www.africau.edu/images/default/sample.pdf\" target=_blank\" id=\"urlLink\"> easy details given</a>)";
var capturePattern = @"(. )\(<a .*href.*?=""(.*?)"".*>(.*)</a>\)";
var regex = new Regex(capturePattern);
var captures = regex.Match(myMessage);
var newString = $"{captures.Groups[1]}{captures.Groups[2]}{captures.Groups[3]}";
Console.WriteLine(myMessage);
Console.WriteLine(newString);
輸出:
請訪問該網站以獲取更多資訊(<a href="http://www.africau.edu/images/default/sample.pdf" target=_blank" id="urlLink"> 給出了簡單的詳細資訊)
請在網站上獲取更多資訊http://www.africau.edu/images/default/sample.pdf給出簡單的詳細資訊
當然,正則運算式只和你能想到/測驗的情況一樣好。我快速寫了這個只是為了說明,所以一定要驗證其他字串變體。
另一種方法是使用HTMLAgilityPack:
string myMessage = "Please the website for more information (<a class=\"link\" href=\"http://www.africau.edu/images/default/sample.pdf\" target=_blank\" id=\"urlLink\"> easy details given</a>)";
var doc = new HtmlDocument();
doc.LoadHtml(myMessage);
var prefix = doc.DocumentNode.ChildNodes[0].InnerText;
var url = doc.DocumentNode.SelectNodes("//a[@href]").First().GetAttributeValue("href", string.Empty);
var suffix= doc.DocumentNode.ChildNodes[1].InnerText doc.DocumentNode.ChildNodes[2].InnerText;
var newString = $"{prefix}{url}{suffix}";
Console.WriteLine(myMessage);
Console.WriteLine(newString);
輸出:
請訪問該網站以獲取更多資訊(<a href="http://www.africau.edu/images/default/sample.pdf" target=_blank" id="urlLink"> 給出了簡單的詳細資訊)
請訪問網站了解更多資訊(http://www.africau.edu/images/default/sample.pdf給出簡單的詳細資訊)
Notice this method preserves the parenthesis around the link. This is because from the agility pack's perspective, the first parenthesis is part of the text of the node. You can always remove them with a quick replace.
This method adds a dependency but this library is very mature and has been around for a long time.
it goes without saying that for both methods, you should make sure to add [error handling] checks for unexpected conditions.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360392.html
上一篇:列印時如何加長并保持精度
下一篇:為JSON的屬性提供值
