-1

I have an string that may have several combinations of ANDs and ORs as follow:

   string display = "every ((Id2=Event(MachineInfo='M3',UserInfo='U3')) or (Id1=Event(MachineInfo='M2',UserInfo='U2')) and (Id0=Event(MachineInfo='M1',UserInfo='U1')))"

Right now I have an inefficient way to catch just the first MachineInfo and UserInfo but I don't know how to get the rest of the elements of the string:

 const string machineinfo = @"MachineInfo='(?<MachineInfo>[^']+)";
 const string userinfo = @"UserInfo='(?<UserInfo>[^']+)";

 string machineName = display.Contains("MachineInfo") ?
             Regex.Matches(display, machineinfo)[0].Groups[1].Value : "ANY";
 string userName = display.Contains("UserInfo") ?
             Regex.Matches(display, userinfo)[0].Groups[1].Value : "ANY";

What can I do to get all the MachineInfos and UserInfos without knowing the number of ANDs and ORs that I may have in display string??

Cœur
  • 37,241
  • 25
  • 195
  • 267
Manolete
  • 3,431
  • 7
  • 54
  • 92

2 Answers2

1

It looks like your syntax includes nesting with parenthesis. I suggest you don't use regexes to parse this, but instead look into using a proper parser. Regular expressions can't really express this nestedness.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
-1

Here is an inefficient/incomplete solution, try yourself to improve the result

        MatchCollection aa = Regex.Matches(display, machineinfo);
        foreach (var aaa in aa) {
            //aaa.ToString() 
            //try some thing with object aaa
        }

in the above code, object aaa will give you MachineInfo..try similar with UserInfo also and put ? operator inside foreach loop and try your skill..

techBeginner
  • 3,792
  • 11
  • 43
  • 59