Ruby Regexp Unescape

May not be a big deal to write it yourself…but I wrote this and was quite useful, where I had to do un-escaping of a Regex Pattern for some reasons…

class Regexp
def unescape
source = self.source.split('')
escape_on = false
unescaped_string = source.inject([]) {|r, char|
if char == "\\" and escape_on == false
escape_on = !escape_on
r << char
escape_on = false

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s