在每个注释行的末尾添加句点(句点)

时间:2012-08-06 13:15:20

标签: php regex eclipse

我正试图解决一些样式问题,并希望添加一个完整的'。'到每一行有一行注释的末尾(包含“//”)。

我原本以为有一种方法可以使用正则表达式来实现。

非常感谢任何帮助 感谢

1 个答案:

答案 0 :(得分:2)

简单方法:

$result = preg_replace('%//.*%', '\0.', $subject);

优雅的方式(如果还没有一个点,最后只添加一个点:

$result = preg_replace('%//.*(?<!\.)$%m', '\0.', $subject);

<强>解释

//      # Match //
.*      # Match any characters except newlines
(?<!\.) # Assert that the last character isn't a dot
$       # right before the end of the line
相关问题