使用gsub和regex的Ruby

使用gsub和regex的Ruby,ruby,regex,gsub,Ruby,Regex,Gsub,好吧,我试着用gsub和regex来替换整个单词,而不是它的一部分。规则之一是将“be”改为b。但我只想为单个单词做这件事 原始字符串:我真的想在所有方面都做到最好 修改过的字符串:我真的想在所有方面都做到最好 所需字符串:我真的希望在所有方面都做到最好 以下代码将接收字符串数组,并应将“be”更改为“b” 如果我不使用regex进行替换,它会给我这样的信息:我真的很想在任何地方使用bst你可以按如下方式执行 rules.default_proc = ->(_,k) { k } arr =

好吧,我试着用gsub和regex来替换整个单词,而不是它的一部分。规则之一是将“be”改为b。但我只想为单个单词做这件事

原始字符串:
我真的想在所有方面都做到最好

修改过的字符串:
我真的想在所有方面都做到最好

所需字符串:
我真的希望在所有方面都做到最好

以下代码将接收字符串数组,并应将“be”更改为“b”


如果我不使用regex进行替换,它会给我这样的信息:
我真的很想在任何地方使用bst
你可以按如下方式执行

rules.default_proc = ->(_,k) { k }
arr = array.map { |s| s.gsub(/\w+/, rules) }
这将产生以下结果

arr.each { |s| puts s }
  # Hey guys, can anyone teach me how 2 b cool? 
  # I really want 2 b the best @ everything,
  # u know what I mean? Tweeting is super fun u guys!!!!
  # OMG u guys, u won't believe how sweet my kitten is. 
  # My kitten is like super cuddly & 2 cute 2 b believed right?
  # I'm running out of example tweets 4 u guys, which is weird, 
  # because I'm a writer & this is just writing & I tweet all day. 
  # For real, u guys. For real.
  # GUISEEEEE this is so fun! I'm tweeting 4 u guys & this tweet is 
  # SOOOO long it's gonna b way more than u would think twitter can handle, 
  # so shorten it up u know what I mean? I just can never tell how long 2 keep typing!
  # New game. Middle aged tweet followed by #youngPeopleHashTag Example: 
  # Gotta get my colonoscopy & mammogram soon. Prevention is key! #swag

我曾经在
规则
上附加一个proc,这样
规则[k]
如果
规则
没有键
k
,就返回
k
,并使用使用的形式使用哈希进行替换。

/\bword\b/
查找单词
单词
,不适用于由变量
word
定义的字符串。只需在代码中将这个正则表达式更改为
/\b#{pattern}\b/
或可能的
/\b#{pattern}\b/i
(不区分大小写),您的方法就可以工作了

此取代基在不更改原始阵列的情况下输出新阵列:

def substitutor(strings,rules)
  rules.inject(strings) do |strings, (pattern, replace)|
    strings.map do |string|
      string.gsub(/\b#{pattern}\b/i, replace)
    end
  end
end

puts substitutor(array,rules)

# Hey guys, can anyone teach me how 2 b cool? 
# I really want 2 b the best @ everything,
# u know what I mean? Tweeting is super fun u guys!!!!
# OMG u guys, u won't believe how sweet my kitten is. 
# My kitten is like super cuddly & 2 cute 2 b believed right?
# I'm running out of example tweets 4 u guys, which is weird, 
# because I'm a writer & this is just writing & I tweet all day. 
# 4 real, u guys. 4 real.
# GUISEEEEE this is so fun! I'm tweeting 4 u guys & this tweet is 
# SOOOO long it's gonna b way more than u would think twitter can handle, 
# so shorten it up u know what I mean? I just can never tell how long 2 keep typing!
# New game. Middle aged tweet followed by #youngPeopleHashTag Example: 
# Gotta get my colonoscopy & mammogram soon. Prevention is key! #swag

该方法的形式参数是错误的。它是
string
,但根据方法定义,它应该是
strings
(复数)。您当前的正则表达式在单词的两侧查找单词边界,看起来是正确的。请显示您实际用于生成输出的确切代码。
Regexp.union
是您的朋友。另一种方法:在空格上拆分为单词,然后分别重新映射,
连接回单个字符串。此外,您的regexp不插入变量
单词
,它包含文字字符串单词。
def substitutor(strings,rules)
  rules.inject(strings) do |strings, (pattern, replace)|
    strings.map do |string|
      string.gsub(/\b#{pattern}\b/i, replace)
    end
  end
end

puts substitutor(array,rules)

# Hey guys, can anyone teach me how 2 b cool? 
# I really want 2 b the best @ everything,
# u know what I mean? Tweeting is super fun u guys!!!!
# OMG u guys, u won't believe how sweet my kitten is. 
# My kitten is like super cuddly & 2 cute 2 b believed right?
# I'm running out of example tweets 4 u guys, which is weird, 
# because I'm a writer & this is just writing & I tweet all day. 
# 4 real, u guys. 4 real.
# GUISEEEEE this is so fun! I'm tweeting 4 u guys & this tweet is 
# SOOOO long it's gonna b way more than u would think twitter can handle, 
# so shorten it up u know what I mean? I just can never tell how long 2 keep typing!
# New game. Middle aged tweet followed by #youngPeopleHashTag Example: 
# Gotta get my colonoscopy & mammogram soon. Prevention is key! #swag