我正在尝试从文件中提取所有块注释并将它们显示在一个数组中:
我的示例文本如下所示:
/*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[SOME OTHER CONTENT IS HERE]
/*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[SOME OTHER CONTENT IS HERE]
//*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[SOME OTHER CONTENT IS HERE]
我尝试做一个preg_match_all,以获取/ *和* /之间的所有内容,但它只返回空。这是我的代码:
preg_match_all("'\^\s*/*!(.*?)*\/'si", $source, $m);
list(,$data['description']) = $m;
这是我第一次尝试使用正则表达式,所以如果解决方案是直接的,我会道歉,我看了很多教程,但无法弄清楚我做错了什么。 TIA!
答案 0 :(得分:0)
你忘了逃避之前的*!而最后,也不应该逃避^
preg_match_all("'^\s*/\*!(.*)\*\/'si", $source, $m);
答案 1 :(得分:0)
使用此正则表达式:
preg_match_all("/\/\*[\s\S]+?\*\//", $source, $m);
我使用[\s\S]+
来匹配换行符。
Array
(
[0] => Array
(
[0] => /*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[1] => /*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[2] => /*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
)
)
答案 2 :(得分:0)
看起来你已经有了几个解决方案,但我还是认为我的2美分也是如此。你可以试试这个:
preg_match_all('~(/\*!.*?\*/)~s', $string, $matches);
print_r($matches[0]);
输出:
Array
(
[0] => /*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[1] => /*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
[2] => /*! @brief some example text
*
* @param some example text
* @return some example text
* @todo documentation to be done.
*/
)
REGEX的说明:
( /\*! .*? \*/ )
^ ^ ^
1 2 3