Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby using gsub with regex

Tags:

regex

ruby

gsub

Okay so I tried using gsub with regex to replace only the entire word, and not part of it. One of the rules is to change "be" to b. But I only want to do it for the individual words.

Original string: I really want to be the best at everything

Modified string: I really want to be the best at everything

Desired string: 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".

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

like image 410
Jun Huang Avatar asked Dec 13 '25 14:12

Jun Huang


1 Answers

/\bword\b/ looks for the word word, not for the string defined by the variable 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.

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
like image 116
Eric Duminil Avatar answered Dec 15 '25 13:12

Eric Duminil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!