Regex for variable declaration and initialization in c#

后端 未结 4 524
生来不讨喜
生来不讨喜 2020-12-18 16:50

I want to write a RegEx to pull out all the variable values and their names from the variable declaration statement. Say i have

int i,k = 10,l=0

i want to wr

4条回答
  •  执念已碎
    2020-12-18 17:13

    Start thinking about the structure of a definition, say,

    (a line can start with some spaces) followed by,
    
    (Type) followed by
    
    (at least one space)
    (variable_1)
    (optionally
       (comma // next var
        |
        '='number // initialization
        ) ...`
    

    then try to convert each group:

    ^      \s*    \w+           \s+        \w+         ?          (','    |  '=' \d+   ) ...
    line  some    type          at least  var          optionally   more  or init some
    start spaces  (some chars)  one space (some chars)              vars     val  digits
    

    Left as homework to remove spaces and fix up the final regex.

提交回复
热议问题