Regex for variable declaration and initialization in c#

后端 未结 4 531
生来不讨喜
生来不讨喜 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:16

    Try this:

     ^(int|[sS]tring)\s+\w+\s*(=\s*[^,]+)?(,\s*\w+\s*(=\s*[^,]+)?)*$
    

    It'll match your example code

    int i,k = 10,l=0
    

    And making a few assumptions about the language you may or may not be using, it'll also match:

    int i, j, k=10, l=0
    string i=23, j, k=10, l=0
    

提交回复
热议问题