在Ruby中搜索和替换字符串的最快方法?

时间:2014-04-08 22:02:44

标签: ruby-on-rails ruby regex string

我正在构建一个清理用户生成内容的库,并且需要进行数千次字符串替换(性能很关键)。

最快在字符串中进行搜索和替换的方式是什么?

以下是图书馆替换的示例:

u2 => you too
2day => today
2moro => tomorrow
2morrow => tomorrow
2tomorow  => tomorrow

有四种情况可以显示字符串:

  • 字符串中的起始字(末尾有空格,但不在其前面)2day sample
  • 字符串的中间(前面和后面有空格)sample 2day sample
  • 字符串的结尾(前面只有一个空格,但是是最后一个字)sample 2day
  • 整个字符串是匹配2day

即。正如<{1}}

这样的单词中间的正则表达不应该替换它

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"