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

**URL:** <https://rubytalk.org/t/how-to-substitute-group-of-words-in-a-line-with-another-text/52654>\
**Category:** ruby-talk\
**Created:** [3 April 2009 18:10 UTC](https://rubytalk.org/t/how-to-substitute-group-of-words-in-a-line-with-another-text/52654 "2009-04-03T18:10:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muralidharan\_Regupat](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muralidharan\_Regupat](https://rubytalk.org/u/Muralidharan_Regupat)\
**Post date:** [3 April 2009 18:10 UTC](https://rubytalk.org/t/how-to-substitute-group-of-words-in-a-line-with-another-text/52654/1 "2009-04-03T18:10:46Z")

</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)

&nbsp;&nbsp;&nbsp;&nbsp;@smsdictionary.each\_pair do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>key, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpvalue = value.chomp.downcase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg=message.chomp.downcase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "looking for \<#{tmpvalue}\> in \<#{msg}\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newmsg=msg.gsub(tmpvalue,key)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts newmsg  
end

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

thanks,  
murali

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [3 April 2009 18:39 UTC](https://rubytalk.org/t/how-to-substitute-group-of-words-in-a-line-with-another-text/52654/2 "2009-04-03T18:39:33Z")

</div>

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

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

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 03.04.2009 20:10, Muralidharan Regupathy wrote:
> 
> > 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)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@smsdictionary.each\_pair do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>key, value|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpvalue = value.chomp.downcase  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg=message.chomp.downcase  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "looking for \<#{tmpvalue}\> in \<#{msg}\>"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newmsg=msg.gsub(tmpvalue,key)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts newmsg  
> > end
> > 
> > the above mentioned code is not working, any help is appreciated

---

<div class="post-metadata">

**Author:** ![Muralidharan\_Regupat](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muralidharan\_Regupat](https://rubytalk.org/u/Muralidharan_Regupat)\
**Post date:** [3 April 2009 19:06 UTC](https://rubytalk.org/t/how-to-substitute-group-of-words-in-a-line-with-another-text/52654/3 "2009-04-03T19:06:04Z")

</div>

Robert Klemme wrote:

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>key, value|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpvalue = value.chomp.downcase  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg=message.chomp.downcase  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "looking for \<#{tmpvalue}\> in \<#{msg}\>"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newmsg=msg.gsub(tmpvalue,key)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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|  
> &nbsp;&nbsp;&nbsp;@smsdictionary[match]  
> end
> 
> Kind regards
> 
> &nbsp;&nbsp;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)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg=message.chomp.downcase  
&nbsp;&nbsp;&nbsp;&nbsp;@smsdictionary.each\_pair do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>key, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpvalue = value.chomp.downcase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg=msg.sub(tmpvalue,key)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts msg  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> > On 03.04.2009 20:10, Muralidharan Regupathy wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
