我将SQL表中的无序列表存储为字符串。我需要稍后在Excel文件中显示它,但我在正确格式化它时遇到了问题。
我尝试过使用正则表达式,我认为我已经接近了,但是我遗漏了一些东西。
这是我的示例输入字符串
<ul>
<li>Line 1</li>
<li>Line 2</li>
<li>Line 3</li>
<li>Line 4
<ul style="list-style-type:circle">
<li>Line 4-1</li>
<li>Line 4-2
<ul style="list-style-type:square">
<li>Line 4-2-1</li>
<li>Line 4-2-2</li>
<li>Line 4-2-3</li>
</ul>
</li>
<li>Line 4-3</li>
</ul>
</li>
<li>Line 5</li>
<li>Line 6</li>
<li>Line 7</li>
</ul>这就是我到目前为止所做的。
var dt = new DataTable();
dt.Columns.Add();
string inputValue; //unordered list from above
Regex rgxLI = new Regex(@"<li>(.*?)</li>");
Regex rgxCircle = new Regex(@"<ul style=\""list-style-type:circle\"">(.*?)</ul>");
Regex rgxSquare = new Regex(@"<ul style=\""list-style-type:square\"">(.*?)</ul>");
MatchCollection mcLI = rgxLI.Matches(inputValue);
for (var i = 0; i < mcLI.Count; i++)
{
DataRow dr = dt.NewRow();
//string instructionLine = mc[i].Value;
if (mcLI[i].Value.Contains("<ul style=\"list-style-type:circle\">"))
{
MatchCollection mcCircle = rgxCircle.Matches(mcLI[i].Value);
for (var j = 0; j < mcCircle.Count; j++)
{
if (mcLI[j].Value.Contains("<ul style=\"list-style-type:square\">"))
{
MatchCollection mcSquare= rgxSquare.Matches(mcLI[j].Value);
dr[0] = System.Net.WebUtility.HtmlDecode("▪" + mcSquare[j].ToString().Replace("<li>", "").Replace("</li>", ""));
}
else
{
dr[0] = System.Net.WebUtility.HtmlDecode("•" + mcCircle[j].ToString().Replace("<li>", "").Replace("</li>", ""));
}
}
}
else
{
dr[0] = System.Net.WebUtility.HtmlDecode(mcLI[i].Value.Replace("<li>", "").Replace("</li>", ""));
}
dt.Rows.Add(dr);
}不知道我是不是把事情搞得太复杂了,或者只是错过了一些步骤。我可以得到大部分的字符串分析,但我错过了要点后,4-1。
发布于 2019-08-14 19:18:46
你可以试试这个:
List<string> list = new List<string>();
list = (Regex.Split(YOURSTRING, "\r\n")).ToList<string>();这应该被每一行分开。它将分裂成一个数组,这就是我使用.ToList()的原因。
"\r\n“用于使用Regex查找换行符。
https://stackoverflow.com/questions/57500664
复制相似问题