在每一行中我想解析标签后面的字符串
<li>602 — <a href="/w/index.php?title=Text602&action=edit&redlink=1" class="new" title="Text602 (page does not exist)">Text602</a> document</li>
<li>ABW — <a href="/wiki/AbiWord" title="AbiWord">AbiWord</a> Document</li>
我想解析第一行的 602 和第二行的 ABW。 我试图做的是:
private void ParseFilesTypes()
{
string[] lines = File.ReadAllLines(@"E:\New folder (44)\New Text Document.txt");
foreach (string str in lines)
{
int r = str.IndexOf("<li>");
if (r >= 0)
{
int i = str.IndexOf(" -", r + 1);
if (i >= 0)
{
int c = str.IndexOf(" -", i + 1);
if (c >= 0)
{
i++;
MessageBox.Show(str.Substring(i, c - i));
}
}
}
}
}
但是c总是-1
最佳答案
我认为正则表达式会很有用(除非没有 li
属性):
var regex = new Regex("^<li>(.+) —");
foreach (string str in lines)
{
var m = regex.Match(str);
if (m.Success)
MessageBox.Show(m.Groups[1].Value);
}
关于c# - 我如何从读取所有行的文本文件中解析特定字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38031721/