Regexes: non-greedy and . matching whitespaces – Rubular to the rescue!

So some weeks ago (unfortunately I didn’t really have the time to post this immediately)  I ran into the following problem, I wanted to match the following snippet:

Shoes.setup do
gem 'twitter'
gem 'oauth'
gem 'launchy'
gem 'feedzirra'

I wanted to do this in order to transform a (red) Shoes application to a green shoes application (where the gems are handled by ruby gems as you are used to, as it uses a normal ruby interpreter). So how do you match this with a regex? I ran into 2 little problems.

First: I wanted to match Shoes.setup do – end, and everything in between. Unfortunately “.” just matches any single character, no whitespaces. After a bit of google action I found out that the m-switch exists to alter this behavior for the regex. So there we go…

But what is this? It replaced the whole file! How could that be? Oh yeah, underneath there is a whole program with its own “end” – and regexes are by default greedy and try to match as many characters as possible. So everything in between Shoes.setup do (the first thing in the program) and the last end… so how to make a matcher non greedy? Just add a question mark (?) after the matcher!


So in the end the regex looks like this:

/Shoes\.setup do.*?end/m

I could have known this way faster if I would have just stick with my favorite Ruby Regex reference, Rubular! It has an amazing short reference and now is also linked at the Resources page.


One thought on “Regexes: non-greedy and . matching whitespaces – Rubular to the rescue!

  1. Common problem and neat solution 🙂 I just saw you’re collecting on your blog resources to make coding and learning to code easier. As a great resource to understand regular expressions I would recommend you a great book by Jeffrey E. F. Friedl – “Mastering Regular Expressions”. I’m amused by the way the author explains the way you should think about regular expressions. It’s already a time investment to go through this book but, at least from my perspective, it’s 100% worth doing.

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