C# 中变量声明和初始化的正则表达式
我想编写一个正则表达式来从变量声明语句中提取所有变量值及其名称。 假设我有
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
例如,开始考虑定义的结构,
然后尝试转换每个组:
作为作业留下来删除空格并修复最终的正则表达式。
Start thinking about the structure of a definition, say,
then try to convert each group:
Left as homework to remove spaces and fix up the final regex.
以下是一些有用的信息,您可以使用
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
您可以从 [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.
试试这个:
它将匹配您的示例代码
并对您可能使用或可能不使用的语言做出一些假设,它也会匹配:
Try this:
It'll match your example code
And making a few assumptions about the language you may or may not be using, it'll also match: