Interesting - Making ".*" in regular expressions less greedy
Sometimes .* in regular expressions matches more than we would like it too. Let's say you have a string that includes some numbers you want to extract from a string. Our string might look like this:
some text 123 more text
now we might try to use the following regular expression to get the 123.
.*([\d]+)
However, this will only get us the 3. This happens because .* will match as match as possible. Adding a ? will change this to only match what is needed.
.*?([\d]+)
Now we get the 123 as desired.
A nice toll to experiment with regular expressions is rubular.com. Rubular allows you to see the effects of your regex in combination with an example string instantly.
