in reply to Re: Regexp oddity
in thread Regexp oddity
In this case, the first (\s*) will be greedy and attempt to match as many characters as possible. $first will contain 3 spaces, a tab, and 3 more spaces. $second will contain 3 spaces. However, by adding the question mark, we make it non-greedy.# 3 spaces, a tab, 3 more spaces, another tab and 3 more spaces (repre +sent by chr() for clarity) $test = chr(32)x3 . chr(9) . chr(32)x3 . chr(9) . chr(32)x3; ($first = $1, $second = $2) if $test =~ /(\s*)\t(\s*)/;
This means that (\s*?) attempt the smallest match possible that satisfies that above regex. In this case, $first contains 3 spaces and $second contains 3 spaces, a tab, and 3 more spaces. The '?' does not mean "aka zero".($first = $1, $second = $2) if $test =~ /(\s*?)\t(\s*)/;
Incidentally, most regexes ending in (.*?)$/ (like the one in the original post) have a superfluous ? because there is no way to make that statement non-greedy, since it's forced to match to the end.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: Re: Regexp oddity
by Adam (Vicar) on Jun 21, 2000 at 20:57 UTC | |
by Ovid (Cardinal) on Jun 21, 2000 at 21:03 UTC |