I need to find regex to parse a variable

0

I need to parse a C code using regex and I'm having difficulty knowing if a variable is getting a float or integer value.

Ex:

valor_01 = ( 5 * 3 ) / 2.5 + ( 4 % 3 ) ^ 4 ;
  • valor_01 can be any variable name, something like \w+
  • I need to catch if after = and before ; has some decimal value ( 2.5 in this example)
  • So far I've got the following expression:
  • (\w+\s?\=\s?).+
    

    Problem: With this expression I get the name of the variable, the equal and everything I have on the line and I can not find out if it has a (\d+.\d+) and a ; at the end of the line.

    It's like I need to capture

      

    value_01 = 2.5;

        
    asked by anonymous 19.11.2018 / 00:04

    1 answer

    0

    I do not see the need to validate the variable name. Variable names can not contain points, if there is a point anywhere in the string, there is a float.

    Anyway, if you want to validate the entire expression:

    \w+ letters or numbers or underscores

    \s* followed or not by spaces

    = followed by an equal

    [\d\+\-\*\/\s]* followed or not by numbers, algebraic expressions and spaces

    \.\d followed by a period followed by a number

    [\d\+\-\*\/\s]* followed or not by numbers, algebraic expressions and spaces

    ; followed by a semicolon

    That is \w+\s*=[\d\+\-\*\/\s]*\.\d[\d\+\-\*\/\s]*;

        
    19.11.2018 / 01:24