使用php regex(preg_replace)擦除多行#define宏

时间:2014-03-10 20:04:38

标签: php regex macros preg-replace

我试图用PHP regexp

擦除C文件中的多行宏

这个例子之一

this one should stay
#define HI    something is here ()d5gf           \
dgkdflgj \
it ends here
this one should stay
        #define HII something is here 2
this one should stay

输出应如下所示:

this one should stay
this one should stay
this one should stay

我正在使用此正则表达式,但它无法正常工作:

preg_replace("/#define[^\n\\]+(\\\n[^\n\\])*/u", "", $input_lines);

注意:该文件是UTF-8,这就是你在preg_replace中的原因。

1 个答案:

答案 0 :(得分:1)

您可以使用此模式:

$pattern = '~#define(?=\s)(?>.*\\\\\R)*.*\R?~u';
$result = preg_replace($pattern, '', $code);