1

I have expression like this

 R403[i] == 3 and R404[i] == 2 AND R405[i] == 1 and R403[i+1] == 5 and R404[i+1] == 2 AND R405[i+1] == 1 R231[2]

And I want get all occurence of my expression using this regex

[R|M|B].*?\]

But one condition again that I want to apply it must contains variable i inside so R121[1] would not be capture.

Anyone can help?

DrKoch
  • 8,904
  • 2
  • 30
  • 43
mrhands
  • 1,317
  • 4
  • 17
  • 39
  • Hint `[R|M|B]` is the same as `[BMR|]`. Read about how [character classes](http://www.regular-expressions.info/refcharclass.html) work. – Tomalak Feb 11 '15 at 08:11

2 Answers2

2

If i understood your question correctly this would be the regex:

Regex reg = new Regex(@"[RMB]\d{3}\[.*?i.*?\]");

If your "i" less expressions can come before the ones with an i use instead:

Regex reg = new Regex(@"[RMB]\d{3}\[[^]]*?i[^]]*?\]");
Florian Schmidinger
  • 4,377
  • 2
  • 13
  • 25
0

Try this solution. It will check for group match of the pattern.

 string pattern = @"[RMB]\d{3}\[\d*\]";
 string input = " M403[50] == 3 and R404[i] == 2 AND R405[i] == 1 and R403[i+1] == 5 and R404[i+1] == 2 AND R405[i+1] == 1 R231[2]";

 foreach (Match match in matches)
 {
      Console.WriteLine("Mactch:        {0}", match.Value);
      Console.WriteLine();
  }
Kavindu Dodanduwa
  • 9,413
  • 2
  • 26
  • 40