正则表达式去除 phpdoc 多行注释

发布于 2024-08-30 14:40:25 字数 828 浏览 7 评论 0原文

我有这个:

/**
 * @file
 * API for loading and interacting with modules.
 * More explaination here.
 *
 * @author  Reveller <me@localhost>
 * @version 19:05 28-12-2008
 */

我正在寻找一个正则表达式来删除除 @token 数据之外的所有数据,所以结果是:

@file API for loading and interacting with modules. More explaination here.
@author Reveller <me@localhost>
@version 19:05 28-12-2008

我现在有这个:

$text = preg_replace('/\r?\n *\* */', ' ', $text);

它部分完成了工作:它只删除了每行前面的 * 。谁能帮助我,让它也去掉 /** 和最后的斜杠 /?任何帮助将不胜感激!

PS:例如,如果 commentlbock 包含类似的内容

/**
 * @foo Here's some slashes for ya: / and \
 */

,那么显然 @foo 之后的斜杠可能不会被删除。结果一定是:

@foo Here's some slashes for ya: / and \

我希望有一个正则表达式大师:-)

I have this:

/**
 * @file
 * API for loading and interacting with modules.
 * More explaination here.
 *
 * @author  Reveller <me@localhost>
 * @version 19:05 28-12-2008
 */

I'm looking for a regex to strip all but the @token data, so the result would be:

@file API for loading and interacting with modules. More explaination here.
@author Reveller <me@localhost>
@version 19:05 28-12-2008

I now have this:

$text = preg_replace('/\r?\n *\* */', ' ', $text);

It does the job partially: it only removes the * in front of each line. Who could help me so it also strips /** and the final slash /? Any help would be greatly appreciated!

P.S: If, for instance, the commentlbock would contain something like

/**
 * @foo Here's some slashes for ya: / and \
 */

Then obviously the slashes after @foo may not be stripped. The reult would have to be:

@foo Here's some slashes for ya: / and \

I hope there's a regex guru out there :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧街凉风 2024-09-06 14:40:25

Try

$result = preg_replace('%(\r?\n(?! \* ?@))?^(/\*\*\r?\n \* | \*/| \* ?)%m', ' ', $subject);

It 会在每行的开头插入一个额外的空格,因此您可能需要在第二步中删除前导空格。

说明:

(\r?\n(?! \* ?@))?:如果可能,匹配换行符,除非后面跟着 * @

^< /code>: 断言以下匹配从行的开头开始

(: 匹配

/\*\*\r?\n \*: /**<换行> *

|

\*/: */

|:或

\* ?*,可选地后跟另一个空格

):交替序列结束

Try

$result = preg_replace('%(\r?\n(?! \* ?@))?^(/\*\*\r?\n \* | \*/| \* ?)%m', ' ', $subject);

It will insert an extra space at the start of each line, so you might want to strip leading whitespace in a second step.

Explanation:

(\r?\n(?! \* ?@))?: If possible, match a newline unless it's followed by * @

^: Assert that the following match starts at the beginning of the line

(: Either match

/\*\*\r?\n \*: /**<newline> *

| or

\*/: */

|: or

\* ?: *, optionally followed by another space

): End of alternation sequence

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