如何通过正则表达式去除MySQL的可执行注释
我需要在 mysqldump 结果中获取可执行注释的内容,但是对于正则表达式
/\/\*\!\d+\s+(.*?)\*\//s
和这样的输入数据:
/*!50003 text
some text else
/*
comment
also comment
*/
text...
and also text...
*/
我得到错误的结果,因为它仅从“文本”到“也注释”行获取数据。如何跳过评论进入评论? 谢谢。
UPD:我不能使用“^”和“$”来标记输入的开始和结束,因为我在输入中有很多可执行语句。
UPD2:我想要的输出:
text
some text else
/*
comment
also comment
*/
text...
and also text...
并非所有输入方式都在下面的评论中。我认为这很奇怪,得到与输入相同的输出。
UPD3: 可执行注释的开头必须是 /*!ANYNUMBER。它必须被跳过并且不包含在输出中。可执行注释的结尾只是 */ 正确的输出示例在“UPD2”中给出。
I need to get content of executable comments in mysqldump results, but for regexp
/\/\*\!\d+\s+(.*?)\*\//s
and input data like this:
/*!50003 text
some text else
/*
comment
also comment
*/
text...
and also text...
*/
I get wrong result because it get data only from "text" to "also comment" lines. How can I to skip comment into comment?
Thanks.
UPD: I cannot use "^" and "$" to mark start and end of input, because I have a lot ot executable statements in input.
UPD2: Output I want:
text
some text else
/*
comment
also comment
*/
text...
and also text...
NOT all input how in comment below. It's very strange, I think, get the same output as input.
UPD3:
Start of executable comment must be /*!ANYNUMBER. It must be skipped and not included in output. End of executable comment is simply */ Right output example is presented in "UPD2".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
纯正则表达式无法处理嵌套,但 PHP 的风格可以通过使用 递归。使用 PCRE_EXTENDED 修饰符 这样我们就可以有空格和注释:
简而言之:
使用中:
结果:
Pure regular expressions can't handle nesting, but PHP's flavor can by using recursion. Using the PCRE_EXTENDED modifier so we can have whitespace and comments:
In short:
In use:
Result:
基于这个优秀的解决方案,我做了一个 PHP 正则表达式来删除所有类型的注释(只有注释,而不是看起来像注释的引用文本;):
匹配 MySQL 评论的正则表达式
Based on this excellent solution I've done a PHP regexp to remove all type of comments (and only comments, not quoted text looking like comments ;):
Regex to match MySQL comments