# Hashes and Regexes and Special Characters

**URL:** <https://www.ruby-forum.com/t/hashes-and-regexes-and-special-characters/136399>\
**Category:** Rails\
**Created:** [May 1, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/hashes-and-regexes-and-special-characters/136399 "2008-05-01T18:20:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![armstheykill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@armstheykill](https://www.ruby-forum.com/u/armstheykill)\
**Post date:** [May 1, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/hashes-and-regexes-and-special-characters/136399/1 "2008-05-01T18:20:27Z")

</div>

I am rolling my own smiley/emoticon generator. A lot of this has do  
do with my less than stellar knowledge of ruby, so if this is better  
asked on a ruby list, let me know and I’ll move along…

I have text based emoticon replacement working fine. I use a hash to  
define the text/image pairs like this…

emotes = {  
:wow =\> “wow.gif”,  
:lol =\> “lol.gif”  
}

I then have a simple regex that goes through the text and plucks out  
[:key] and replaces it with an image tag.

The problem is I would also like to do conversion of 🙂 😮 ☹ and  
such. I’m running into trouble setting up my hash like that.

I can’t seem to use special characters like : or ( in a key. So I  
actually made two hashes, one with description/emote\_text pairs and  
one with description/image pairs like so…

special\_keys = {  
:smile =\> “:)”  
:frown =\> "☹  
}  
special\_emotes = {  
:smile =\> “regular\_smile.gif”  
:frown =\> “frown.gif”  
}

Then I thought I would regex something like

special\_keys.each\_value do |key|  
text=text.gsub(/#{key}/,""  
end

Now, forgetting for the moment the right side of that regex, which I  
don’t know if it will even work or not, the regex is failing because  
the ‘)’ in #{key} is closing the regex early. So I thought I would  
get smart and escape the special characters in special\_keys like so:

special\_keys = {  
:smile =\> “:)”  
}

Which, to my suprise ends up as {:smile=\>":)"}

Foo!

About this time I figured I was trying too hard and that there’s  
probably something shiny in ruby that will solve this little problem.  
Anyone out there an expert hash wrangler?

TIA!

---

<div class="post-metadata">

**Author:** ![armstheykill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@armstheykill](https://www.ruby-forum.com/u/armstheykill)\
**Post date:** [May 1, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/hashes-and-regexes-and-special-characters/136399/2 "2008-05-01T18:44:40Z")

</div>

On 1 May 2008, at 17:19, removed\_email\_address@domain.invalid wrote:

> Which, to my suprise ends up as {:smile=\>":)"}
> 
> Foo!
> 
> About this time I figured I was trying too hard and that there’s  
> probably something shiny in ruby that will solve this little problem.  
> Anyone out there an expert hash wrangler?

I reckon the reason that happens is because in a string, ) is a  
perfectly valid character but ) is an unknown character escape and so  
the \ gets dropped.  
You really don’t want to be doing this by hand though, RegExp.quote  
will do that for you.  
Lastly you can have a symbol with anything you want in it, but you  
need to say :‘here is a weird symbol’

Fred

---

<div class="post-metadata">

**Author:** ![armstheykill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@armstheykill](https://www.ruby-forum.com/u/armstheykill)\
**Post date:** [May 1, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/hashes-and-regexes-and-special-characters/136399/3 "2008-05-01T19:52:37Z")

</div>

Frederic,

Thank you SO much for your help. I managed to turn the mess of code I  
was writing into something very simple and rubyish.

# I now have the following in my helper…

unescaped\_emotes ={  
:“:)” =\> “smile.gif”,  
:“:(” =\> “frown.gif”  
}  
emotes ={  
:lol =\> “laugh.gif”,  
:wow =\> “jawdrop.gif”  
}

emotes.each\_key do |emote|  
text=text.gsub(/[:#{Regexp.escape(emote.to\_s)}]/,“\<img src=  
"#{emotes[emote]}"\>”)  
end  
unescaped\_emotes.each\_key do |emote|  
text=text.gsub(/#{Regexp.escape(emote.to\_s)/,“\<img src=  
"#{unescaped\_emotes[emote]}"\>”)  
end  
return text

==================================

So sweet.

I decided to keep two seperate hashes for the emotes, to make it more  
clear and “pretty”. Any key in unescaped\_emotes will be replaced as  
is, any key in emotes will be replaced if it appears in the text in  
the form [:key].

Thanks a ton for your nudge in the right direction.

—A

On May 1, 12:43 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
