Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C正则表达式-不获取外部组_C#_Regex - Fatal编程技术网

C# C正则表达式-不获取外部组

C# C正则表达式-不获取外部组,c#,regex,C#,Regex,我使用以下正则表达式来查找组 string pattern = @"(?<member>(?>\w+))\((?:(?<parameter>(?:(?>[^,()""']+)|""(?>[^\\""]+|\\"")*""|@""(?>[^""]+|"""")*""|'(?:[^']|\\')*'|\((?:(?<nest>\()|(?<-nest>\))|(?>[^()]+))*(?(nest)(?!))\))+)\s

我使用以下正则表达式来查找组

string pattern = @"(?<member>(?>\w+))\((?:(?<parameter>(?:(?>[^,()""']+)|""(?>[^\\""]+|\\"")*""|@""(?>[^""]+|"""")*""|'(?:[^']|\\')*'|\((?:(?<nest>\()|(?<-nest>\))|(?>[^()]+))*(?(nest)(?!))\))+)\s*(?(?=,),\s*|(?=\))))+\)";
我得到了以下几组:

一个GetValueGetValue1+2*GetValue3*4

b GetValueGetValue5*6/7

我正在获取除外部组GetValue之外的所有组…./8是不是得到了


模式中可能存在什么问题?

我对您的最佳帮助是下载并使用此RegexDesigner


因为它是一个复杂的正则表达式,所以最好为您的搜索字符串提供一个简单而实际的示例。我发现在大多数情况下,你需要一个贪婪的正则表达式匹配

例如:

Non-Greedy:
"a.+?b":

Greedy:
"a.*b":

如果您试图进行以下匹配,则仅使用正则表达式是不可能的:

GetValueGetValue1+2*GetValue3*4/GetValueGetValue5*6/7/8 GetValueGetValue1+2*GetValue3*4 GetValue1+2 GetValue3*4 GetValueGetValue5*6/7/8 GetValue5*6/7 看看原因。但是,您可以使用递归来获取匹配中的匹配项,比如未经测试的伪代码:

private List<string> getEmAll(string search)
{
    var matches = (new Regex(@"Your Expression Here")).Match(search);
    var ret = new List<string>();
    while (matches.Success)
    {
        ret.Add(matches.Value);
        ret.AddRange(getEmAll(matches.Value));
        matches = matches.NextMatch();
    }
    return ret;
}

...

getEmAll("GetValue(GetValue(GetValue(1 + 2) * GetValue(3 * 4)) / GetValue(GetValue(5 * 6) / 7) / 8)");

如果您想将匹配项进一步划分为匹配组,则会稍微复杂一些,但您可以理解要点。

我尝试创建一个regexr,但它完全不匹配:您能进一步解释所需的结果吗?具体来说,您希望在每个组中捕获什么:成员、参数、嵌套、-nest。
private List<string> getEmAll(string search)
{
    var matches = (new Regex(@"Your Expression Here")).Match(search);
    var ret = new List<string>();
    while (matches.Success)
    {
        ret.Add(matches.Value);
        ret.AddRange(getEmAll(matches.Value));
        matches = matches.NextMatch();
    }
    return ret;
}

...

getEmAll("GetValue(GetValue(GetValue(1 + 2) * GetValue(3 * 4)) / GetValue(GetValue(5 * 6) / 7) / 8)");