简体   繁体   English

Ruby使用带有正则表达式的gsub

[英]Ruby using gsub with regex

Okay so I tried using gsub with regex to replace only the entire word, and not part of it. 好的,所以我尝试使用带有正则表达式的gsub来替换整个单词,而不是它的一部分。 One of the rules is to change "be" to b. 其中一条规则是将“be”改为b。 But I only want to do it for the individual words. 但我只想为个别单词做这件事。

Original string: I really want to be the best at everything 原始字符串: I really want to be the best at everything

Modified string: I really want to be the best at everything 修改后的字符串: I really want to be the best at everything

Desired string: I really want to b the best at everything 期望的字符串: I really want to b the best at everything

The following code will take in an array of strings and should change "be" to "b". 以下代码将接受一个字符串数组,并应将“be”更改为“b”。

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

rules = {
    "hello" => "hi",
    "too" => "2",
    "to" => "2",
    "two" => "2",
    "for" => "4",
    "four" => "4",
    "be" => "b",
    "you" => "u",
    "at" => "@",
    "and" => "&"
}

def substitutor(strings,subs)
    strings.each_with_index do |string,index|
        subs.each do |word,substitute|
            strings[index].gsub!(/\bword\b/, substitute)
        end
    end
end

substitutor(array,rules)

If I substituted without using regex, it would give me this: I really want to b the bst at everything 如果我不使用正则表达式替换,它会给我这个: I really want to b the bst at everything

/\\bword\\b/ looks for the word word , not for the string defined by the variable word . /\\bword\\b/查找word ,而不是查找变量word定义的字符串。 Just change this regex to /\\b#{pattern}\\b/ or possibly /\\b#{pattern}\\b/i (case insensitive) in your code, and your method will work. 只需在代码中将此正则表达式更改为/\\b#{pattern}\\b/或可能/\\b#{pattern}\\b/i (不区分大小写),您的方法就可以正常工作。

This substituor outputs a new array, without changing the original one : 此替换器输出一个新数组,而不更改原始数组:

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

You can do that as follows. 你可以这样做。

rules.default_proc = ->(_,k) { k }
arr = array.map { |s| s.gsub(/\w+/, rules) }

which produces the following. 产生以下内容。

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

I used Hash#default_proc= to attach a proc to rules so that rules[k] returns k if rules does not have a key k and used the form of String#gsub that employs a hash for making substitutions. 我使用Hash#default_proc =将proc附加到rules以便rules[k]返回k如果rules没有密钥k并使用String#gsub的形式,它使用哈希进行替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM