Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Standup 11/9/2010

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.


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.


Now we get the 123 as desired.

A nice toll to experiment with regular expressions is Rubular allows you to see the effects of your regex in combination with an example string instantly.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *