解析 C++ 的正则表达式枚举
如何构造正则表达式来解析 C++ 枚举? 我尝试过的枚举看起来像是
enum Temperature
{
C = 0,
F=1, // some elements are commented
R, // most elements are not gived a value
K // sometimes the last element is succeeded by a comma
} temperature;
// different indent style is used
enum Depth {
m = 0,
ft = 1,
} depth;
我尝试了几种简单的模式,但没有一个足够通用来捕获上述所有情况。
任何正则表达式向导可以帮助我吗?
编辑:澄清一下,我想要名称和值,例如 C 和 0。
How can a regular expression be constructed to parse C++ enums?
The enums I tried on looked like
enum Temperature
{
C = 0,
F=1, // some elements are commented
R, // most elements are not gived a value
K // sometimes the last element is succeeded by a comma
} temperature;
// different indent style is used
enum Depth {
m = 0,
ft = 1,
} depth;
I tried several simple patterns but none is general enough to catch all cases above.
Any regexp wizard who can help me?
Edit: to clarify, I want the name and value, e.g. C and 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很有挑战性:)以下是我能想到的最好的。假设只给出了 { 和 } 之间的文本,它会捕获所有名称和相应的值:
That was challenging :) Below is the best I could come up with. Assuming it is given just the text between { and } it captures all names and corresponding values:
如果我们使用正则表达式来匹配枚举而不是使用它来解析枚举。我认为这是可能的。尝试执行以下步骤:
步骤 1。确保C/C++源代码能够编译成功。
步骤2。从 C/C++ 源代码中删除所有注释。
步骤3。匹配枚举
一个可行的 Ruby 示例代码:
输出:
If we use regex to match enum rather than use it to parse enum. I think it is possible. try with these steps:
step1. make sure the C/C++ source code can be compile successful.
step2. strip all comments from the C/C++ source code.
step3. match enum
a workable Ruby sample code:
output: