Two people questioned it explicitly; one of them said I was missing the ^ anchor.($wanted) = $line =~ /(\S*)/;
This is perhaps the trickiest lesson to learn about regexes, partly because it's so simple and in its simplicity it is incorrectly interpreted. The * modifier matches 0 or more. It will always succeed, and it will always try to take as much as it can and still have the rest of the regex succeed. Furthermore, it will match as early in the string as possible.
In "this is cool", the regex matches "this". In " is this cool?" it matches nothing, but successfully. Why doesn't it match "is"? Because the \S* matches at the very beginning of the string, where there are (quite plainly) zero non-whitespace characters. The "classic" example of this trickery is:
$_ = "fred xxxxxxx barney"; s/x*//; print; # fred xxxxxxx barney
_____________________________________________________
Jeff japhy Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
In reply to Shouldn't \S* be ^\S* by japhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |