Last Updated: February 25, 2016
·
1.139K
· nimf

Regex operators made non greedy

Imagine, you need to quickly match a text inside <b> tags. And you don't want to load any XML/HTML parsing libraries.

I first came up with:

str = "Lorem <b>ipsum</b> dolor sit <b>amet</b>, consectetur adipiscing elit."
str.scan /<b>.*<\/b>/
 => ["<b>ipsum</b> dolor sit <b>amet</b>"]

which match everything from first <b> to last </b>.
And this is not what I want.

This happens because the star operator is greedy by default.
To make it ungreedy (or reluctant) simply add question mark behind:

str.scan /<b>.*?<\/b>/
 => ["<b>ipsum</b>", "<b>amet</b>"]

That's better.
You can also apply this to operators: +, {} and question mark itself.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside