如何通过正则表达式去除MySQL的可执行注释

发布于 2024-12-26 17:31:23 字数 637 浏览 0 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2025-01-02 17:31:23

纯正则表达式无法处理嵌套,但 PHP 的风格可以通过使用 递归。使用 PCRE_EXTENDED 修饰符 这样我们就可以有空格和注释:

%(               # opening RE delimiter, group start
  /\*            # comment open marker
    (  [^/*]     # non comment-marker characters
     | /(?!\*)   # '/' not followed by '*', so not a comment open
     | \*(?!/)   # '*' not followed by '/', so not a comment close
     | (?R)      # recursive case
    )*           # repeat any number of times
  \*/            # comment close marker
)%x              # group end, closing RE delimiter, PCRE_EXTENDED

简而言之:

%(/\*([^/*]|/(?!\*)|\*(?!/)|(?R))*\*/)%x

使用中:

<?php

$commentRE = '%(/\*([^/*]|/(?!\*)|\*(?!/)|(?1))*\*/)%';
$doc = <<<EOS

USE database;

/* comment
and a
/* nested comment /* me too */
   now exiting
 */
the comment */


/*!50003 text
some text else
/*
comment
also comment
*/
text...
and also text...
*/

CREATE TABLE IF NOT EXISTS ...

EOS;

preg_match_all($commentRE, $doc, $parts);
var_export($parts[0]);

结果:

array (
  0 => '/* comment
  and a
  /* nested comment /* me too */
     now exiting
   */
  the comment */',
  1 => '/*!50003 text
  some text else
  /*
    comment
    also comment
  */
  text...
  and also text...
*/',
)

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:

%(               # opening RE delimiter, group start
  /\*            # comment open marker
    (  [^/*]     # non comment-marker characters
     | /(?!\*)   # '/' not followed by '*', so not a comment open
     | \*(?!/)   # '*' not followed by '/', so not a comment close
     | (?R)      # recursive case
    )*           # repeat any number of times
  \*/            # comment close marker
)%x              # group end, closing RE delimiter, PCRE_EXTENDED

In short:

%(/\*([^/*]|/(?!\*)|\*(?!/)|(?R))*\*/)%x

In use:

<?php

$commentRE = '%(/\*([^/*]|/(?!\*)|\*(?!/)|(?1))*\*/)%';
$doc = <<<EOS

USE database;

/* comment
and a
/* nested comment /* me too */
   now exiting
 */
the comment */


/*!50003 text
some text else
/*
comment
also comment
*/
text...
and also text...
*/

CREATE TABLE IF NOT EXISTS ...

EOS;

preg_match_all($commentRE, $doc, $parts);
var_export($parts[0]);

Result:

array (
  0 => '/* comment
  and a
  /* nested comment /* me too */
     now exiting
   */
  the comment */',
  1 => '/*!50003 text
  some text else
  /*
    comment
    also comment
  */
  text...
  and also text...
*/',
)
苍白女子 2025-01-02 17:31:23

基于这个优秀的解决方案,我做了一个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文