I want (in C#) to check the syntax and extract some data from a string.
Check if the string contains: "someWord IS someWord( OR someWord){1-infinite}"
And extract every words and for the first word, name the group "switch"
This is my string :
string text = "[bird] IS blue OR yellow OR green";
So I use this regex
string switchPattern = @"\s*(?<switch>.+?)\s+IS\s+(.+?)(?:\s+OR\s+(.+?))+$";
And extract with
Match switchCaseMatch = Regex.Match(text, switchCaseOperatorPattern);
This give me a group with 4 elements
[0]: [bird] IS blue OR yellow OR green
[1]: green
[2]: blue
[3]: [bird] named switch
but I want
[0]: [bird] IS blue OR yellow OR green
[1]: green
[2]: yellow
[3]: blue
[4]: [bird] named switch
I hoped that the last "(.+?)" will create a group for all matching cases, but it create only one, for the last occurence. I try with Regex.Matches with the same result.
I know that I could do it with two regex (a Regex.Match then Regex.Matches for the "someWord( OR someWord){1-infinite}"), but I want to know if is it possible to do it with only one regex.
Thanks
Regex.Capturesinstead ofRegex.Groupsmight give you what you want. See this article