Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Greedy vs Lazy in Regular Expressions

The greedy expression can be seen as a True *and* False predicate, meaning true while the token is valid, while being false if the pattern matching hasn't been exhausted.
The lazy expression is the True *or* False predicate, meaning true while the token is valid, or false if the pattern match hasn't been fully exhausted.

irb(main):037:0> "<EM>first</EM>"[/<.+>/] #greedy 
=> "<EM>first</EM>"
irb(main):038:0> "<EM>first</EM>"[/<.+?>/] #lazy
=> "<EM>"
irb(main):039:0> "<EM>first</EM>"[/<[^<>]+>/] # better solution
=> "<EM>"

source: Regular Expression Quick Start [regular-expressions.info]
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS