简体   繁体   English

删除html标签并在c#中找到结束标签时将其拆分

[英]Remove html tags and split it when end tag was found in c#

I want to remove all html tags from the following string and split it without using the period(full stop) as matching character. 我想从以下字符串中删除所有html标签,并在不使用句点(句号)作为匹配字符的情况下进行拆分。 The following sting is dynamic one which can have more conditions inside list tag 以下字符串是动态的,可以在列表标记中包含更多条件

<li>This Offer cannot be redeemed with any other offer.</li><li>Only one Offer can be used at a time.</li><li>This Offer is not transferable.</li><li>......</li><li>....</li</ul></div>

I'm Expecting the following relult 我期待以下结果

  1. This Offer cannot be redeemed with any other offer. 此优惠不能与其他任何优惠一起使用。
  2. Only one Offer can be used at a time. 一次只能使用一个优惠。
  3. This Offer is not transferable. 此优惠不可转让。
  4. .... ....
  5. .... ....
String[] myString = yourString.replace("<li>", "").Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);

try this 尝试这个

const string HTML_TAG_PATTERN = "<[^/li]>"; // may require some change
string safeString = Regex.Replace(yourString, HTML_TAG_PATTERN, string.Empty);
String[] myString = safeString.Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);

you can try this regex too 您也可以尝试此正则表达式

string acceptable = "li";
string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*?\1?)?)*\s*/?>";
string yourString= Regex.Replace(yourString, stringPattern, string.Empty);
String[] myString = yourString.replace("<li>", "").Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);

you can remove all html tag and split also by below code 您可以删除所有html标记并按以下代码进行拆分

string HTML_TAG_PATTERN = "<.*?>";
string str = @"<li>This Offer cannot be redeemed with any other offer.</li><li>Only one Offer can be used at a time.</li><li>This Offer is not transferable.</li><li>......</li><li>....</li</ul></div>";
string[] stString = Regex.Replace(str.Replace("</li>", "#$#"), HTML_TAG_PATTERN, string.Empty).Split("#$#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

If you are able to give id to your <li> then , You can try with javascript code something like following>> 如果您可以给<li>指定ID,则可以尝试使用如下的javascript代码>>

var str=doccument.getElementById("liID").innerHTML;

This thing you can try on windows onload event or any specific according to your application. 您可以根据应用程序尝试Windows onload事件或任何特定事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM