I have to find the code blocks from the given code using a regex in C#.
e.g. I have to find the For loop block from the following code
For A in 1..10 Loop stmt1; For C in cur_op Loop stmt2; end loop; end loop; For T in 4..8 loop stmt3; end loop;
I want to retrieve the code blocks as
For A in 1..10 Loop stmt1; For C in cur_op Loop stmt2; end loop; end loop;
and
For T in 4..8 loop stmt3; end loop;
Can anyone suggest me a regex for this?
I don’t think it’s possible. You’re asking for a regex parsing a context-free language, and while Perl REs actually can parse CFLs, I’m not sure C# regular expressions can do it, and using it is not the biggest pleasure out there.
Natural solution for your problem would be to create a parser for the language, and get the info from that. You could use a parser generator like CoCo/R or ANTLR.