Rubeque string substitution





I’ve decided to start sharing how I solve problems occasionally on this blog. I think this will be helpful for potential employers to see how I work.

The following picture is of a problem

Screen Shot 2013-08-22 at 1.57.57 PM

So we want to substitute the matched pattern of a string within the main string, for a new string. Lets start with making the pattern and actual regular expression.

pattern =

This Regexp will be used in Strings sub method(String#sub) in order to replace the matched contents of a string into the replacement parameters value,


Well that was easy, the first case passes, but the second one doesn’t. This is because sub only replaces the first matched instance it finds. Instead we’ll use String#gsub, for global sub. This will replace all instances of the matched data.


That makes the next test pass, however, we’re having issues with getting the string “??” to work like we want it to as a regular expression. This is because ? is a special character used inside regular expressions(It matches either one or none of the preceding character). is a great way to check and or just reference ruby regular expressions. So we need to escape these characters.  We can do this with Regexp.escape(pattern), this “Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string” (taken from ruby-docs). So instead of .new we will use .escape right? No, if you look closely at that definition, .escape returns a string, we still need pattern to be a regexp. So we will make it work like this:

pattern =

That’s a lot better, now we only have the last test case to solve. our last test case replaces the matched data with a string that contains special substitution cases. ex: \1, \2, \3. These cases will return the exact Match data returned from the regexp. \1 return the first matched case, \2 return the second, and so on. How do we get this to just return \2. we can’t escape the use Regexp.escape(replace), that will just return silly backslashes all over the place. Instead we have to look closer at String#sub and String#gsub. “The problem is that when using sub (and gsub), without a block, ruby interprets special character sequences in the replacement parameter” this quote was taken from two-bit-fool’s answer on Stack Overflow. So when ruby sees the replacement parameter it just INTERPRETS the \1\2\3 and does some magic. We don’t want that, so lets put the replacement string into a block instead of letting it be a paramter.

self.gsub(pattern) { replace}

Now everything is passing! So this is how I solved that question. However, Lets do some recursion on this and  shorten this up a bit with regexp interpolation. With pattern we could do what we did, or we could just create a regexp and interpolate the string returned from .escape like so

pattern = /#{Regexp.escape(pattern)}/

That’s easy enough to understand, and its short enough that I think it could go on the same line, and we can skip redefining the pattern variable all together. So to sum it all up, my final answer was this:

self.gsub(/#{Regexp.escape(pattern)}/ ) {replace}  

I simplified this a little better, I skipped some steps in my head but thought this would clear up how I solved each assertion.


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 )

Google+ photo

You are commenting using your Google+ 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