【问题标题】:Regex doesn't find a match with a pattern正则表达式找不到与模式的匹配项
【发布时间】:2015-08-29 12:15:15
【问题描述】:

在一个 C# 项目中,Regex 的行为对我来说很奇怪。

我有这个方法:

string RegTest()
    {
        string HTML = "<input type=\"hidden\" name=\"authenticity_token\" value=\"d27956cca6b75db4d8dd502d0569dd246455131c\">";
        Regex AuthRegex = new Regex(@"name=""authenticity_token"" value=""([A-Ba-b0-9/-]+)""");
        string Auth = AuthRegex.Match(HTML).Value;
        return Auth;
    }

由于我完全不明白的原因,Regex 找不到与此模式的任何匹配项。它只返回""

我该如何解决这个问题?

【问题讨论】:

  • @ndn 我认为通过使用@ 我不需要逃避它。在模式中转义它的正确方法是什么?

标签: html .net regex


【解决方案1】:

问题是:

[A-Ba-b0-9/-]+

字符范围 (x-y) 基本上做的是获取介于两者之间的所有字符的集合。换句话说,a-b = 在ab 之间的所有字母,也就是只有ab。不过,

d27956cca6b75db4d8dd502d0569dd246455131c

看起来像一个十六进制。因此,您应该使用

[A-Fa-f0-9-]+

改为。

【讨论】:

  • 好的,非常感谢!现在我还注意到我写了A-B 而不是像我通常那样写A-Z... -_-
猜你喜欢
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多