Friday, February 13, 2015

Regex Non-Greedy Matching

Yea... it's been awhile and apologies. Lately been overloaded with work that I'm just too tired at the end of the day...

Anyway this is going to be short post and more of a note to myself. Needed to do some regex in order to remove the [] sections from a string like this: ab[123]cde[456]fgh

\[.*\] gives me abfgh

To get a non-greedy match, you need to append ? after .* so:

\[.*?\] yields abcdefgh