我正在构建一个清理用户生成内容的库,并且需要进行数千次字符串替换(性能很关键)。
最快在字符串中进行搜索和替换的方式是什么?
以下是图书馆替换的示例:
u2 => you too
2day => today
2moro => tomorrow
2morrow => tomorrow
2tomorow => tomorrow
有四种情况可以显示字符串:
2day sample
sample 2day sample
sample 2day
2day
即。正如<{1}}
这样的单词中间的正则表达不应该替换它答案 0 :(得分:2)
可能的解决方案:
replaces = {'u2' => 'you too', '2day' => 'today', '2moro' => 'tomorrow'}
str = '2day and 2moro are u2 sample2daysample'
#exp = Regexp.union(replaces.keys) #it is the best but to use \b this should be a quiet different
exp = Regexp.new(replaces.keys.map { |x| "\\b" + Regexp.escape(x) + "\\b" }.join('|'))
str = str.gsub(exp, replaces)
# => "today and tomorrow are you too sample2daysample"