我正在尝试解决一些样式检查问题,并希望在具有单行注释(包含"//")的每行末尾添加一个句号'.)。
我本以为有一种方法可以使用正则表达式来做到这一点。
任何帮助将不胜感激谢谢
简单的方法:
$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