用正则表达式替换字符串,并替换一些单词与数组匹配

时间:2015-04-18 21:41:41

标签: ruby regex

我有一个字符串:

str = "John: hey, what's your name?.. :haha \n Stella: :foo :xx: my name is ... stella :xx:"

我想用ary = [":haha", ":xx:", ":foo", ":bar"]替换列表(.*)中的所有表情符号和特殊字符(空格除外),以便它变成这样:

John(.*) hey(.*) what(.*)s your name(.*) Stella(.*) my name is (.*) stella (.*)

enter image description here

我试过了:

str.gsub(Regexp.new("^#{ary.join('|')}$")) { |w| "(.*)" }.gsub( /[\W ]+/, "(.*)")
# => "John(.*)hey(.*)what(.*)s(.*)your(.*)name(.*)haha(.*)Stella(.*)my(.*)name(.*)is(.*)stella(.*)"

问题:

  • 空间仍然取代

2 个答案:

答案 0 :(得分:1)

我尝试过创建一种更通用的方法,但最终提出了一个三步法。由于似乎无法过滤掉多个连续的(.*),因此我添加了第3个gsub的后期处理:

str = "John: hey, what's your name?.. :haha \n Stella: :foo :xx: my name is ... stella :xx:"
ary = [":haha", ":xx:", ":foo", ":bar"]
print str.gsub(Regexp.new("#{ary.join('|')}")) { |w| "(.*)" }.gsub( /(?>\(\.\*\)|[^\w ]+)/, "(.*)").gsub(/\(\.\*\)(?>\s*\(\.\*\))*/,"(.*)")

输出sample program

John(.*) hey(.*) what(.*)s your name(.*) Stella(.*) my name is (.*) stella (.*)

答案 1 :(得分:0)

你可以这样做:

s = "John: hey, what's your name?.. :haha \n Stella: :foo :xx: my name is ... stella :xx:"

r = /\?\.\. :haha \n|: :foo :xx:|\.\.\.|:xx:|[^\w ]/

s.gsub(r,'(.*)')
  #=> "John(.*) hey(.*) what(.*)s your name(.*) Stella(.*) my name is (.*) stella (.*)" 

唯一棘手的问题涉及正则表达式中'或'元素的顺序。特别是,在替换其他三个字符串之前,:无法替换。