Statement regex

I am trying to write a regular expression to define an if statement. The only problem I am facing is grabbing it if the brackets are in brackets. For example:

if (condition_function(params)) {
     statements;
}

My expression for capturing all if statements except them:

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

Does anyone know how to write this?

+5
source share
8 answers

I think it might work. If someone sees something that I do not do as a reason, this will not work, answer.

if\s*\(((?:[^\(\)]|\((?1)\))*+)\)\s*{((?:[^{}]|{(?2)})*+)}

The only problem this should happen now is if the if statement has an if statement.

if, , , , , - , .

: . , , ​​ . :

if (var1 == "("){
    echo "{";
}

if. :

if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*

if, , .

UPDATE: , else else if, if. , , if. , , .

if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*;

, : http://gskinner.com/RegExr/

+5

, , , , ( dirkgently dmckee).

WP: , ...

Btw. , , ([[][]] , []][ is not), "" , .

+13

? .

+5

Turing. , , Flex. , , . #, .

public void TestIf()
    {
      var s = @"if (condition_function(params)) {
     statements;
       }";
      var ifRegex = @"if *\(.*\) *{.*}";
      if (Regex.IsMatch(s, ifRegex, RegexOptions.Singleline))
      {
        var firstParens = s.IndexOf('(');
        if (firstParens != -1)
        {
          var conditionPart = s.Skip(firstParens + 1);
          int stack = 1;
          int lastParens = -1; 
          while(stack > 0)
          {
            for (int i = 0; i < conditionPart.Count(); i++)
            {
              var c = conditionPart.ElementAt(i);
              if (c == '(')
              {
                stack++;
              }
              if (c == ')')
              {
                stack--;
              }
              if (stack == 0)
              {
                lastParens = i;
                break; 
              }
            }
          }
          if (lastParens != -1)
          {
            var condition = conditionPart.Take(lastParens);
            Console.WriteLine(new string(condition.ToArray()));
          }
        }
      }
    } 
+3
r = /\bif\s*\(/

txt = <<TXT
if(test)
if (test)
if  (xyz)
; if
print x if(true)
TXT

p txt.scan(r)

if (something).. - .. , , .

?

+1

, , :

if\s*\(((?!\s*\{).+)\)\s*\{(.|\s)*?\}

((?!\s*\{).), ) ( , "{! regexp )

0

...

if\s*?\(.*?)\s*?(({?\s*?(.*?;)+\s*?})|(.*?;))
0

Modified regex (Koukaakiva) to find without brackets - https://regex101.com/r/fE6hA5/1 and names are added to the template

-1
source

All Articles