I have a header file which contains several struct definitions.
I want to extract a spesific struct from this file. For example
I need to get only second struct.
typedef struct {
int a;
int b;
} first;
typedef struct {
int x;
int y;
} second;
Below expression
...
headerText = headerFile.read()
re.match(r"typedef struct {(.*)} second;", headerText)
...
returns
int a;
int b;
} first;
typedef struct {
int x;
int y;
How can I get only the second struct?
Thanks…
Use this
regex..just replace thesecondwith any other required struct name[without groups]
Use this regex if you dont want the groups
The above regex matches
typedef structthat is not followed by anothertypedef structi.e.(?!.*?typedef struct)..This is done so that multiple structs are not captured[with groups]
Basically the above
regexmatches a struct that is either at the start of the file or it would match the required struct after a statement terminator..i.e;.This is done so that other struct definitions are not included in the result..(?:^|\;)matches the start of the string or a;and doesnt get captured in the group.*matches 0 to many characters greedily(typedef struct.*?\}.*?\bsecond\b.*?;)now matches your struct.*?matches 0 to many characters lazilyGroup1now has your required struct