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.
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|$)|)/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With