Reputation: 820
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...
Upvotes: 1
Views: 59
Reputation: 32807
Use this regex
..just replace the second
with any other required struct name
[without groups]
Use this regex if you dont want the groups
typedef struct(?!.*?typedef struct).*?\}.*?\bsecond\b.*?;
The above regex matches typedef struct
that is not followed by another typedef struct
i.e. (?!.*?typedef struct)
..This is done so that multiple structs are not captured
[with groups]
(?:^|\;).*(typedef struct.*?\}.*?\bsecond\b.*?;)
Basically the above regex
matches 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 lazily
Group1
now has your required struct
Upvotes: 1