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' end
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: