# How to substitute group of words in a line with another text

**URL:** <https://www.ruby-forum.com/t/how-to-substitute-group-of-words-in-a-line-with-another-text/164669>\
**Category:** Ruby\
**Created:** [April 3, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/how-to-substitute-group-of-words-in-a-line-with-another-text/164669 "2009-04-03T20:11:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mregupathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5fc32e/32.png) [@mregupathy](https://www.ruby-forum.com/u/mregupathy)\
**Post date:** [April 3, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/how-to-substitute-group-of-words-in-a-line-with-another-text/164669/1 "2009-04-03T20:11:12Z")

</div>

I am trying to create a routine which will convert text to SMS lingo  
format. For example a sentence might contain “This should be done as  
soon as possible”. I have created a HASH which stores “ASAP” =\> “as soon  
as possible”.  
My routine should parse the message and subsitute with “ASAP” and return  
a string “This should be done ASAP”.

def SMSdeflater(message)

```
@smsdictionary.each_pair do
  |key, value|
  tmpvalue = value.chomp.downcase
  msg=message.chomp.downcase
  puts "looking for <#{tmpvalue}> in <#{msg}>"
  newmsg=msg.gsub(tmpvalue,key)
  puts newmsg

```

end

the above mentioned code is not working, any help is appreciated

thanks,  
murali

---

<div class="post-metadata">

**Author:** ![mregupathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5fc32e/32.png) [@mregupathy](https://www.ruby-forum.com/u/mregupathy)\
**Post date:** [April 3, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-substitute-group-of-words-in-a-line-with-another-text/164669/2 "2009-04-03T20:41:13Z")

</div>

On 03.04.2009 20:10, Muralidharan Regupathy wrote:

> ```
> |key, value|
> tmpvalue = value.chomp.downcase
> msg=message.chomp.downcase
> puts "looking for <#{tmpvalue}> in <#{msg}>"
> newmsg=msg.gsub(tmpvalue,key)
> puts newmsg
> 
> ```
> 
> end
> 
> the above mentioned code is not working, any help is appreciated

If there are not many phrases you can use Regexp.union:

msg.gsub Regexp.union(@smsdictionary.keys) do |match|  
@smsdictionary[match]  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mregupathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5fc32e/32.png) [@mregupathy](https://www.ruby-forum.com/u/mregupathy)\
**Post date:** [April 3, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/how-to-substitute-group-of-words-in-a-line-with-another-text/164669/3 "2009-04-03T21:06:35Z")

</div>

Robert K. wrote:

> On 03.04.2009 20:10, Muralidharan Regupathy wrote:
> 
> > ```
> > |key, value|
> > tmpvalue = value.chomp.downcase
> > msg=message.chomp.downcase
> > puts "looking for <#{tmpvalue}> in <#{msg}>"
> > newmsg=msg.gsub(tmpvalue,key)
> > puts newmsg
> > 
> > ```
> > 
> > end
> > 
> > the above mentioned code is not working, any help is appreciated
> 
> If there are not many phrases you can use Regexp.union:
> 
> msg.gsub Regexp.union(@smsdictionary.keys) do |match|  
> @smsdictionary[match]  
> end
> 
> Kind regards
> 
> robert

Thanks Robert  
However i tried otherway and it seems to work fine. I know my solution  
is not a clean one but it works for my learning.  
def SMSdeflater(message)  
msg=message.chomp.downcase  
@smsdictionary.each\_pair do  
|key, value|  
tmpvalue = value.chomp.downcase  
msg=msg.sub(tmpvalue,key)  
puts msg  
end  
end
