需要正则表达式来删除<;选择>;并且<;选项>;html标签
本文关键字:gt lt 并且 选项 html 标签 选择 正则表达式 删除 | 更新日期: 2023-09-27 18:22:33
我正在C#中解析一个html文件,并从html中提取文本。我的html文件中有很多标记。html文件有select标记和option标记。我需要一个正则表达式来从html文件中删除select标记和option标记。我不想要这些信息。所以我想用任何正则表达式删除它。
下面是我想从我的html文件中删除的html:
<select name="state" onchange="setCities();" id="state">>
<option value="CA" selected="selected">CA</option>
<option value="WA">WA</option>
<option value="TX">TX</option>
<option value="NV">NV</option>
<option value="CO">CO</option>
<option value="MI">MI</option>
<option value="SC">SC</option>
您不需要使用RegEx来简单地剥离HTML标记。下面的方法遍历HTML代码字符串,并创建一个不带任何标记的新返回字符串
这种方式也比RegEx快。
public static string StripHTMLTags(string str)
{
char[] array = new char[str.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c == '<')
{
inside = true;
continue;
}
if (c == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = c;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}