正则表达式删除注释和多行注释以及空行 [英] Regex to strip comments and multi-line comments and empty lines

查看:113
本文介绍了正则表达式删除注释和多行注释以及空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个文件,并且想使用php和regex剥离:

I want to parse a file and I want to use php and regex to strip:

  • 空白行或空行
  • 单行注释
  • 多行注释

基本上我想删除任何包含

basically I want to remove any line containing

/* text */ 

或多行注释

/***
some
text
*****/

如果可能,另一个正则表达式检查行是否为空(删除空行)

If possible, another regex to check if the line is empty (Remove blank lines)

有可能吗?有人可以向我发布一个正则表达式吗?

Is that possible? can somebody post to me a regex that does just that?

非常感谢.

推荐答案

$text = preg_replace('!/\*.*?\*/!s', '', $text);
$text = preg_replace('/\n\s*\n/', "\n", $text);

这篇关于正则表达式删除注释和多行注释以及空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆