| |||||||||||
| |||||||||||
Greedy v Global on regular expressions Posted by admin (Graham Ellis), 15 January 2003 Here's a program I wrote today to illustrate default, greedy and global regular expression matching. Global By default, Perl will perform a single match. If you add the g modifier and run the match in a list contest, it will return all non-overlapping matches. If you add the g modifier and run the match in a scalar context, it will return the next match each time you run the match, and a false value when there are no more matches available. Greedy. By default, the counts * + and ? (0 or more, 1 or more, 0 or 1) are greedy. In other words, if there are two possible ways that they can match, they'll go for the longest match. If you add an extra ? - so if you use *? +? or ??, they'll go for the shortest match instead. In all cases, matching starts from the left so that the match you get back will be the leftmost one, except on the second and subsequent calls in a scalar context with the g modifier. Code:
This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |