Ruby和Vim都使用帶替代命令的「g」來表示「全部出現」。 「g」代表什麼?在Ruby的「gsub」和Vim的替代命令中,「g」代表什麼?
具體地,在紅寶石,所述String class有兩個「子」命令:sub
將取代僅第一次出現,並gsub
將取代所有出現。例如:
string = "One potato, two potato, three potato, four."
string.sub('potato','banana') # => "One banana, two potato, three potato, four."
string.gsub('potato','banana') # => "One banana, two banana, three banana, four."
同樣,在Vim中,01會去翻整個文件(這是什麼%
手段),並替換每行一個發生,但:%s/foo/bar/g
會做每一行所有出現。我的猜測是,在這兩種情況下,「g」的意思是「貪婪」,因爲Ruby命令和Vim命令都接受正則表達式,但我對貪婪匹配的理解是「匹配滿足這些條件的最長可能的子串標準,「不」匹配儘可能多的子串。「 (請參閱「Watch Out for The Greediness!」)
也許它意味着「全球」? – 2011-05-25 12:41:25