简体   繁体   English

为什么此正则表达式在RegexBuddy中起作用,但在.NET中却不起作用

[英]Why is this Regular expression working in RegexBuddy but not in .NET

Is there reason following should not work in via .Net but works in RegexBuddy? 请问有什么理由不能通过.Net在RegexBuddy中工作?

String: 串:

formatter:'number',formatoptions:{decimalSeparator:'.',decimalPlaces:2,defaulValue:0}

Expression pattern: 表达方式:

[a-zA-Z]+:({)??([a-zA-Z]+[:](')??[a-zA-Z0-9.,]+(?(3)'|,?),?)+(?(1)}|.)

Produces matches within regex buddy but fail within .net. 在正则表达式伙伴中产生匹配项,但在.net中失败。

private static List<string> GetObjectProps(this string str, out string strWithOutObjectProps)
{
    var lst = new List<String>();
    var temp = str;
    Regex RegexObj = new Regex("[a-zA-Z]+:({)??([a-zA-Z]+[:](')??[a-zA-Z0-9.,]+(?(3)'|,?),?)+(?(1)}|.)");
    Match MatchResults = RegexObj.Match(str);

    while(MatchResults.Success) //fails
    {
        lst.Add(MatchResults.Value);
        temp = MatchResults.Index > 0
                   ? temp.Substring(0, MatchResults.Index - 1) +
                     temp.Substring(MatchResults.Index + MatchResults.Length)
                   : temp.Substring(MatchResults.Index + MatchResults.Length);

        MatchResults = MatchResults.NextMatch();
    }

    strWithOutObjectProps = temp;
    return lst;
}

Solution ! 解决方法!

Turns out this conflict was on a/c of older regexbuddy, latest version pointed out the error for .net simulation as well. 事实证明,这种冲突是由于较旧的regexbuddy造成的,最新版本也指出了.net模拟的错误。

Reworked Expression : 重做的表达式:

new Regex(@"\s?\b[a-zA-Z]+\b:\{
           (?:
             \b[a-zA-Z]+\b:
             (?:[0-9]+|'[.,]?'|'[\w]+'),?
           )+
           \}",
           RegexOptions.IgnorePatternWhitespace);

Allthough this expression is not perfect on a/c of having to make the delimiter optional so as to avoid the trailing delimiter, any ideas so as how to avoid this ? 尽管此表达式在必须使定界符成为可选属性以避免尾随定界符的情况下并不是很完美,但有什么想法可以避免这种情况?

I know that RegexBuddy emulates the .NET Regex engine. 我知道RegexBuddy模拟.NET Regex引擎。 It doesn't actually run it directly. 它实际上并没有直接运行它。 The limitations include a lack of support for balancing groups , for example. 局限性包括缺乏对平衡组的支持 But you may have just stumbled across some other incompatibility. 但是您可能刚发现其他不兼容的地方。 You may want to contact them about it or post on their forum so they can look into it. 您可能需要与他们联系或在他们的论坛上发帖,以便他们进行调查。

As a test you can also try the regular expression with Regex Hero which runs directly off of the .NET Regex engine. 作为测试,您还可以尝试使用Regex Hero (可直接在.NET Regex引擎之外运行)的正则表达式。

After a bit of tweaking this is what I came up with: 经过一些调整后,这就是我想出的:

var reg = new Regex("\s+(?<name>[a-zA-Z]*)\s+:\s+(?<value>('.*')|(\".*\")|[^,}]*)");
var input = "{ decimalSeparator : \",\", decimalPlaces : 2, defaulValue : 0 }";
var matches = reg.Matches(input);
var name = matches[0].Groups["name"];
var value = matches[0].Groups["value"];

I made a few assumptions about what you wanted so let me know if they are wrong. 我对您想要的内容做了一些假设,因此请告诉我它们是否错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM