Regex for variable declaration and initialization in c#

你说的曾经没有我的故事 提交于 2019-11-28 02:09:17

问题


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 write a regex something like int\s^,?|(^,?)* but this will also accept k = 10 i.e. (without int preceding it) Basically idea is If string starts with int then get the variable list seperated by ,

i know to extract csv values, but here my string has some initial value as well. How can i resolve it?


回答1:


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.




回答2:


Here is some useful information which you can use

http://compsci.ca/v3/viewtopic.php?t=6712




回答3:


You could build up your regular expression from the [C# Grammar](http://msdn.microsoft.com/en-us/library/aa664812(VS.71).aspx). But building a parser would certainly be better.




回答4:


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


来源:https://stackoverflow.com/questions/585853/regex-for-variable-declaration-and-initialization-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!