C# 中变量声明和初始化的正则表达式

发布于 2024-07-14 02:48:02 字数 235 浏览 6 评论 0原文

我想编写一个正则表达式来从变量声明语句中提取所有变量值及其名称。 假设我有

int i,k = 10,l=0

我想编写一个正则表达式,例如 int\s^,?|(^,?)* 但这也将接受 k = 10 即(前面没有 int) 基本上的想法是 如果字符串以 int 开头则 获取由 分隔的变量列表,

我知道提取 csv 值,但这里我的字符串也有一些初始值。 我该如何解决它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昔日梦未散 2024-07-21 02:48:02

例如,开始考虑定义的结构,

(a line can start with some spaces) followed by,

(Type) followed by

(at least one space)
(variable_1)
(optionally
   (comma // next var
    |
    '='number // initialization
    ) ...`

然后尝试转换每个组:

^      \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

作为作业留下来删除空格并修复最终的正则表达式。

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.

∝单色的世界 2024-07-21 02:48:02

以下是一些有用的信息,您可以使用

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

Here is some useful information which you can use

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

懵少女 2024-07-21 02:48:02

您可以从 [C# Grammar](http://msdn.microsoft.com/en-us/library/aa664812(VS.71).aspx)。 但构建一个解析器肯定会更好。

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.

时光暖心i 2024-07-21 02:48:02

试试这个:

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

它将匹配您的示例代码

int i,k = 10,l=0

并对您可能使用或可能不使用的语言做出一些假设,它也会匹配:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文