Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# RegEx preventing greedy match with same pattern repeated

In implementing a small script parser I've come across a problem with this sample of code - in wanting to select only the part between and including the "if { }" statements, it's being greedy and selecting all including the last line. I guess what I should be using is a negative lookahead.

if [condition1]
{
  task1
  setparameters{a}
  task2
}

if [condition2]
{
  task3
}

setparameters{b}

Currently, I have:

if\b\s\[.*\]\s\{(\s|.)*\}

I guess it's not as simple as breaking on another 'if' either, as something else may come before then. Is it possible to count an equal number of opening and closing braces? Or is there some other magical way that I can just select one of these 'if' statements?

like image 895
Jamie Barron Avatar asked Nov 02 '22 17:11

Jamie Barron


1 Answers

I ran into a similar problem when I was trying to detect SQL strings (with the possibility of escaped quotes), try the regex: if.*?\{(\{.*?\}|[^}])*+\}

It will match an if followed by the condition up until the first {, then from then on it will continue matching if it encounters either something between a { and } OR anything that is not a }, followed by the final closing }.

I used the possessive quantifier to prevent the possibility of catastrophic backtracking.

like image 102
Song Gao Avatar answered Nov 12 '22 22:11

Song Gao