删除多行#使用php regexp定义宏(preg#u replace)
我试图用PHP regexp删除C文件中的多行宏 这是一个例子删除多行#使用php regexp定义宏(preg#u replace),php,regex,macros,preg-replace,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
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
我正在使用此regexp,但它无法正常工作:
preg_replace("/#define[^\n\\]+(\\\n[^\n\\])*/u", "", $input_lines);
注意:文件为UTF-8格式,这就是为什么preg_中的u会被替换。您可以使用以下模式:
$pattern = '~#define(?=\s)(?>.*\\\\\R)*.*\R?~u';
$result = preg_replace($pattern, '', $code);