【问题标题】:C# Quotes in quotes (Regex Matching)C# 引号中的引号(正则表达式匹配)
【发布时间】:2014-06-21 10:44:42
【问题描述】:

我正在尝试使用 Regex.Match 匹配某些内容,但其中已经包含引号,并且它取消了 Regex.Match 的引号。 我检查了其他线程,但他们的答案对我不起作用。

Match divisionWinsLosses = Regex.Match(website, @"<span style="font-size: 18px; color: #6C0; height: 28px; text-shadow: 0 0 1px #000;">(.*?)</span>");

以防万一。该网站的 HTML 代码是这样的:&lt;span style="font-size: 18px; color: #6C0; height: 28px; text-shadow: 0 0 1px #000;"&gt;[What I want is here]&lt;/span&gt;

【问题讨论】:

  • 如果你真的检查过其他线程,你就会知道parsing html with regex is a really bad idea使用了一个html解析器。 HtmlAgilityPack 很棒。
  • 我实际上确实检查了其他线程并决定使用 HtmlAgilityPack 以外的东西对我来说更容易。不过我试过了..只是个人喜好。

标签: c# html css regex string-matching


【解决方案1】:

您需要转义引号。在字符串文字中,使用\"(例如"My name is \"Bob\".")。在逐字字符串文字中,使用""(例如@"My name is ""Bob"".")。见Strings (C# Programming Guide)

Match divisionWinsLosses = Regex.Match(website, @"<span style=""font-size: 18px; color: #6C0; height: 28px; text-shadow: 0 0 1px #000;"">(.*?)</span>");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多