in reply to assertions help
$str =~ m/^#ifdef\s+($macro)$(.*?)(^#else$)?(.*?)^#endif$/sm;
Combining non-greedy matching with a non-capturing group on the conditional clause will do you:
$str =~ m/^#ifdef\s+($macro)$(.*?)(?:(^#else$)(.*?))?^#endif$/sm;
Note that the lines will still be surrounded by newline characters, as in your original case. To get rid of those, you could instead use:
^#ifdef\s+(ABC)\s+^(.*?)(?:(^#else)\s+^(.*?))?^#endif\s*$
Note ikegami's point above re: nesting - regular expressions are terrible about dealing with nested structures.
|
|---|