正则表达式来解析C ++枚举 [英] Regex to parse C++ enum

查看:238
本文介绍了正则表达式来解析C ++枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构造正则表达式来解析C ++枚举?
我试过的枚举看起来像

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.

任何可以帮助我的regexp向导?

Any regexp wizard who can help me?

编辑:澄清,我想要的名称和价值,例如C和0。

to clarify, I want the name and value, e.g. C and 0.

推荐答案

这是有挑战性的:)下面是我能想出来的最好的。假设它被赋予只是{和} 之间的文本,它会捕获所有名称和相应的值:

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:

/(\w+)\s*(?:=\s*(\d+)|)\s*,?\s*(?:(?:\n|$)|\/\/.*?(?:\n|$)|)/

这篇关于正则表达式来解析C ++枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆