简体   繁体   中英

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?

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.

Using a "?" as a qualifier makes a "*" non-greedy. In fact, it may be better to use "+":

\\[.+?]

As @It'sNotALie said (paraphrased), you would benefit from a bit of explanation:

A sample of a nice tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM