2012-08-06 104 views
-1

我試圖解決一些stylecheck問題並希望添加完整的「。」。到具有單行註釋的每行的末尾(包含「//」)。在每條評論行的末尾添加一個完整的句號(句點)

我原以爲有一種方法可以使用正則表達式來做到這一點。

任何幫助將不勝感激 感謝

+2

IDK你在這裏問什麼...... – Neal 2012-08-06 13:16:39

+1

這沒有任何意義。如果「stylecheck」的意思是「語法高亮」,那麼你就在追風車。 A)這是沒有必要的,B)下載和安裝更好的IDE會更快。 – Matt 2012-08-06 13:17:47

+0

請在這裏添加你的代碼。 – 2012-08-06 13:17:49

回答

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