簡體   English   中英

Ruby使用帶有正則表達式的gsub

[英]Ruby using gsub with regex

好的,所以我嘗試使用帶有正則表達式的gsub來替換整個單詞,而不是它的一部分。 其中一條規則是將“be”改為b。 但我只想為個別單詞做這件事。

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

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

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

以下代碼將接受一個字符串數組,並應將“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)

如果我不使用正則表達式替換,它會給我這個: I really want to b the bst at everything

/\\bword\\b/查找word ,而不是查找變量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

你可以這樣做。

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

我使用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