我有一个字符串:
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 (.*)
我试过了:
str.gsub(Regexp.new("^#{ary.join('|')}$")) { |w| "(.*)" }.gsub( /[\W ]+/, "(.*)")
# => "John(.*)hey(.*)what(.*)s(.*)your(.*)name(.*)haha(.*)Stella(.*)my(.*)name(.*)is(.*)stella(.*)"
问题:
答案 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*\(\.\*\))*/,"(.*)")
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 (.*)"
唯一棘手的问题涉及正则表达式中'或'元素的顺序。特别是,在替换其他三个字符串之前,:
无法替换。