1

I am using the following Regular expression for finding the groups

string pattern = @"(?<member>(?>\w+))\((?:(?<parameter>(?:(?>[^,()""']+)|""(?>[^\\""]+|\\"")*""|@""(?>[^""]+|"""")*""|'(?:[^']|\\')*'|\((?:(?<nest>\()|(?<-nest>\))|(?>[^()]+))*(?(nest)(?!))\))+)\s*(?(?=,),\s*|(?=\))))+\)";

from the expression like

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

I am getting the below groups :

a) GetValue(GetValue(1 + 2) * GetValue(3 * 4))

b) GetValue(GetValue(5 * 6) / 7)

I am getting all the groups but the outer group (GetValue(.... / 8)) is not getting ???

What could be the problem in the pattern ??

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
user904567
  • 67
  • 1
  • 2

3 Answers3

0

My best help for you is to download and use this RegexDesigner

http://www.radsoftware.com.au/regexdesigner/

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
0

Since it's a complicated RegEx, it would be good to have and actual example for your search string. I have found in most of these cases you'll need to have a greedy RegEx match.

For example:

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

Greedy:
"a.*b":
Orin
  • 351
  • 5
  • 13
0

If you are trying to have the following matches, it is not possible with regular expressions, alone:

  1. GetValue(GetValue(GetValue(1 + 2) * GetValue(3 * 4)) / GetValue(GetValue(5 * 6) / 7) / 8)
  2. GetValue(GetValue(1 + 2) * GetValue(3 * 4))
  3. GetValue(1 + 2)
  4. GetValue(3 * 4)
  5. GetValue(GetValue(5 * 6) / 7) / 8)
  6. GetValue(5 * 6) / 7)

See this article for why. You could, however, use recursion to get the matches within your match, like (grossly untested pseudocode):

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)");

If you were wanting to separate out the matches further into matching groups, it would be slightly more complicated - but you get the gist.

Community
  • 1
  • 1
Grinn
  • 5,370
  • 38
  • 51