为什么我的正则表达式找不到任何匹配?

时间:2017-12-21 14:57:04

标签: c# .net regex

所以我的 字符串 与空格和所有内容一样。

            id: 123456789,
            name: 'HappyDev',
            member: false,
            language: 0,
            isLoggedIn: 0

这是我的模式

static string pattern = @"              id: (.*),
                name: (.*),
                member: (.*),
                language: (.*),
                isLoggedIn: (.*)";

然后为了得到我的比赛我这样做..

static Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(myString);
                if (m.Success)
                {
                    Console.WriteLine(m.Value);
                }

出于某种原因,我编译时返回false,即使在我可以测试我的模式的每个网站上,它都会返回与值匹配的内容。 为什么我编译时会返回false?

6 个答案:

答案 0 :(得分:2)

替代解决方案:

模式

(?:: ?)(.*)(?:,)|(?:: ?)(.*)

说明:

1st Alternative (?:: ?)(.*)(?:,)
Non-capturing group (?:: ?)
  : matches the character : literally (case sensitive)
     ? matches the character   literally (case sensitive)
  ? Quantifier — Matches between zero and one times, as many times 
    as possible, giving back as needed (greedy)
  1st Capturing Group (.*)
  .* matches any character (except for line terminators)
  * Quantifier — Matches between zero and unlimited times, as many 
    times as possible, giving back as needed (greedy)
  Non-capturing group (?:,)
    , matches the character , literally (case sensitive)

2nd Alternative (?:: ?)(.*)
Non-capturing group (?:: ?)
  : matches the character : literally (case sensitive)
     ? matches the character   literally (case sensitive)
  ? Quantifier — Matches between zero and one times, as many times 
    as possible, giving back as needed (greedy)
  2nd Capturing Group (.*)
  .* matches any character (except for line terminators)
  * Quantifier — Matches between zero and unlimited times, as many 
    times as possible, giving back as needed (greedy)

你失去了指定ID等的独特性 - 但是你仍然依赖隐式排序的非命名捕获组 - 所以有一些地方需要改进。如果您认为它们可能会跳过params或重新排序它们,我会将命名标识符保留为模式的一部分,并将名称添加到捕获组中,以便它们与排序分离。

答案 1 :(得分:1)

问题是你有不同数量的空格。要在任何情况下忽略此问题,您可以使用模式匹配多个空格:\s+。此外,您应该使用新行的模式替换新行:[\n\r]+(请注意,这将匹配任意数量的新行)

所以你的模式变成了:

static string pattern = @"\s+id: (.*),[\n\r]+\s+name: (.*),[\n\r]+\s+member: (.*),[\n\r]+\s+language: (.*),[\n\r]+\s+isLoggedIn: (.*)";

答案 2 :(得分:1)

有不同的解决方法。这是我的:

string pattern = @"^id:\s*(.+),[\n|\r|\r\n]\s+name:(.+),[\n|\r|\r\n]\s+member:\s+(.+),[\n|\r|\r\n]\s+language:\s+(.+),[\n|\r|\r\n]\s+isLoggedIn:\s+(.+)$";

它将考虑介于两者之间的任何空间以及回车/换行的任何组合。

答案 3 :(得分:0)

var str = @"
    id: 123456789,
    name: 'HappyDev',
    member: false,
    language: 0,
    isLoggedIn: 0";

var matches = Regex.Matches(str, @"(?im)(?'attr'\w+):\s+(?'val'[^,]+)");
if (matches.Count == 0)
    Console.WriteLine("No matches found");
else
    matches.Cast<Match>().ToList().ForEach(m =>
        Console.WriteLine($"Match: '{m.Value}' [Attribute = {m.Groups["attr"].Value}, Value = {m.Groups["val"].Value}]"));

答案 4 :(得分:0)

我在Regex101上试过,你的图案有间距问题(空格数不匹配)。

您可以将以下正则表达式用于空格和新行字符,因此不再需要担心空格的数量:

id: (.*),\s*name: (.*),\s*member: (.*),\s*language: (.*),\s*isLoggedIn: (.*)

答案 5 :(得分:0)

谈论初始代码,检查字符串和模式中的空格量是否相等。此代码找到匹配项:

var myString = 
@"            id: 123456789,
            name: 'HappyDev',
            member: false,
            language: 0,
            isLoggedIn: 0";
string pattern = 
@"            id:.*,
            name: (.*),
            member: (.*),
            language: (.*),
            isLoggedIn: (.*)";

Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(myString);
if (m.Success)
{
    Console.WriteLine(m.Value);
}

但是,您不应该像这样使用它,而是用(+)替换空格或使用此处提供的其他解决方案。