正则表达式不会为同一个数据的工作偶尔
问题描述:
我有以下的正则表达式:正则表达式不会为同一个数据的工作偶尔
<div[^>]*>(?<Value>[^<]*(?:(?!</div)<[^<]*)*)[</div>]*
此正则表达式完美的作品,几乎所有的时间相同的数据集,但有时事实并非如此。
我有下面这段代码:
matchValue = oMatch.Groups["Value"].Value.ToLower();
if ((Regex.Match(matchValue, @"(effective\s*date)").Value).Equals("effective date", StringComparison.OrdinalIgnoreCase) == true || (Regex.Match(matchValue, @"(eff\s*date)").Value).Equals("eff date", StringComparison.OrdinalIgnoreCase) == true)
{
headings = matchValue;
headingsData = oMatch.NextMatch().Value;
}
而且我使用多行作为RegexOptions。
我使用与线程的概念上面的代码
现在,我得到的“标题”和“headingsData”几乎每一次正确的值,但有时我得到的标题正确的值,但对于“headingsData”的价值变化。
有谁能告诉我这种情况的原因吗?
答
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
// All divs that does not contain other divs
string xpath = "//div[not(.//div)]";
bool previousWasHeading = false;
foreach(HtmlNode div in doc.DocumentElement.SelectNodes(xpath))
{
if (previousWasHeading)
{
// Previous <div> was the heading, this one is the heading data.
headingsData = div.Text;
previousWasHeading = false;
break; // Stop after first heading/headingData
}
else if (div.InnerText.Contains("effective date") || div.InnerText.Contains("eff date"))
{
// This this <div> is the heading.
heading = div.Text;
previousWasHeading = true;
}
}
+0
我认为这可以表示为一个单行'// div [not(.// div)] [preceding :: div [not(.// div)] [1] [contains(。,'effective date ')或者包含(。,'eff date')]] - - 但是你必须再次看到输入文档。 – Tomalak
你必须向我们展示你的线程代码,我们有什么用处。通过它的声音..你的线程正在比赛.. –
而当你在它的时候,添加两个或三个段落的解释,为什么你使用正则表达式来解析HTML。 – Tomalak
这也是@Tomalak说的。 –