Search code examples
regexvb.netstring.net-2.0

Complex regex with custom formula builder


I have a string which is like this - MVAL("A","01-01-1900")+MVAL(B,"01-01-1900")+MVAL("C")+MVAL(D). Now I want to extract B AND D out of this using regex because it is the first parameter and it has no quotes around it in both the overloaded version of the functions. Secondly because MVAL function is an overloaded function with two versions like MVAL("A") and MVAL(B,"01-01-1900") how will I find which version of the function is being used.

Please help. I'm using System.Text.RegularExpressions.Regex method.


Solution

  • Is it safe to assume that there will never be a comma after the first parameter unless it's followed by a second parameter? If so, this should be all you need:

    string s = @"MVAL(""A"",""01-01-1900"")+MVAL(B,""01-01-1900"")+MVAL(""C"")+MVAL(D)";
    
    foreach (Match m in Regex.Matches(s, @"MVAL\((\w+)(,)?"))
    {
      Console.WriteLine("First param: {0}\nHas second param? {1}\n",
                        m.Groups[1], m.Groups[2].Success);
    }
    

    output:

    First param: B
    Has second param? True
    
    First param: D
    Has second param? False
    

    If there's no comma, the overall match will still succeed because the comma is optional. But, because the second capturing group didn't participate in the match, its Success property is set to False.

    This regex also assumes there will never be any whitespace within the string, as in your example. This regex allows for whitespace between syntax elements:

    @"MVAL\s*\(\s*(\w+)\s*(,)?