正则表达式去除 phpdoc 多行注释
我有这个:
/**
* @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Try
It 会在每行的开头插入一个额外的空格,因此您可能需要在第二步中删除前导空格。
说明:
(\r?\n(?! \* ?@))?
:如果可能,匹配换行符,除非后面跟着* @
^< /code>: 断言以下匹配从行的开头开始
(
: 匹配/\*\*\r?\n \*
:/**<换行> *
|
或\*/
:*/
|
:或\* ?
:*
,可选地后跟另一个空格)
:交替序列结束Try
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