Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression capturing groups

With the following regular expression:

InitValue\((\w*)\)

and the test string:

InitValue(Input1)

I get the following result:

Full match: InitValue(Input1)
Group1: Input1

With the following regular expression:

InitValue\((\w*)\s*,\s*(\w*)\)

and the test string:

InitValue(Input1, Input2)

I get:

Full match: InitValue(Input1, Input2)
Group1: Input1
Group2: Input2

Now I would like to capture any number of arguments to the InitValue-method. The number of arguments to InitValue are unknown.

Full match: InitValue(Input1, Input2, ..., Inputn)
Group1: Input1
Group2: Input2
....
Groupn: Inputn

Of course I can't repeat the following pattern in my regular expression since I don't know the number of arguments in advance:

\s*,\s*(\w*)

How do I write a regular expression which outputs n number of capture groups?

I use the regular expression in C#-code (Regex, Match)...

like image 903
Andy Rehn Avatar asked Mar 18 '26 22:03

Andy Rehn


2 Answers

It is possible to do this in .NET - you use a single capture Group, and then you access the Group's Captures collection to see all the items it captured, not just the final Value.

You'll have to write a regex that can repeat the argument-matching group, something like

InitValue\((?:(\w+)\s*(?:,(?!\s*\))|(?=\s*\)))\s*)*\)

Have a play around with the Debuggex Demo to get it to match what you want.

like image 85
Rawling Avatar answered Mar 20 '26 11:03

Rawling


static void GetParams()
{
    int x = 0;
    var strings = new[]
    {
        "InitValue()",
        "InitValue(Input1)",
        "InitValue(Input1, Input2, Input3, Input4)"
    };

    var pattern = @"(\w+)\((?:(\w+)(?:,?\s*))*\)";

    foreach (var s in strings)
    {
        WriteLine($"String: '{s}'");
        var match = Regex.Match(s, pattern);
        if (match.Success)
        {
            WriteLine($"\tMethod: '{match.Groups[1].Value}'");
            WriteLine("\tParameters:");
            var captures = match.Groups[2].Captures;
            if (captures.Count > 0)
            {
                x = 0;
                foreach (Capture capture in captures)
                {
                    WriteLine($"\t\tParam {++x}: '{capture.Value}'");
                }
            }
            else
            {
                WriteLine("\t\tNo params found.");
            }

            WriteLine();
        }

        else
            WriteLine("No matches found.");
    }
}

/*
Output:

String: 'InitValue()'
        Method: 'InitValue'
        Parameters:
                No params found.

String: 'InitValue(Input1)'
        Method: 'InitValue'
        Parameters:
                Param 1: 'Input1'

String: 'InitValue(Input1, Input2, Input3, Input4)'
        Method: 'InitValue'
        Parameters:
                Param 1: 'Input1'
                Param 2: 'Input2'
                Param 3: 'Input3'
                Param 4: 'Input4'
*/
like image 45
JohnyL Avatar answered Mar 20 '26 10:03

JohnyL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!