正则表达式剥离phpdoc多行注释 [英] Regex to strip phpdoc multiline comment

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

问题描述

我有这个:

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

我正在寻找一个正则表达式来剥离@token数据以外的所有数据,因此结果将是:

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

我现在有这个:

$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!

PS:例如,如果commentlbock中包含

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

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

然后显然@foo之后的斜杠可能不会被删除。结果必须是:

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 :-)

推荐答案

尝试

$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.

说明:

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

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

^ :声明以下匹配项从行的开头开始

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

:要么匹配

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

|

\ * / * /

| :或

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

\* ?: *, optionally followed by another space

:轮换序列的结尾

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

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