Jeff 'Japhy' Pinyan wrote:
I've seen the response of /-?(?:\d+\.?\d*|\.\d+)/, and while that
does work, it seems too noisy to me. What we would really like to
be able to say is /-?\d*\.?\d*/, but you should be able to see that
could match "-." and "." and "", which we decided aren't legitimite
numbers.
So use a look-ahead.
/(-?(?=.?\d)\d*\.?\d*)/
I disagree.
- It has almost as many characters.
- It's more difficult to read/understand.
- It's slower (see benchmark below).
Personally I avoid extended patterns for those reasons, when a
straight forward regex is sufficient.
*That* regex requires some explanation.
Yep.
Who will give it?
Extended patterns are explained in "perldoc perlre".
This is the benchmark I run:
use Benchmark 'cmpthese';
cmpthese -1, {
ext => sub {'2.3' =~ /-?(?=.?\d)\d*\.?\d*/ and my $x = 1},
normal => sub {'2.3' =~ /-?(?:\d+\.?\d*|\.\d+)/ and my $x = 1}
};
Result:
Rate ext normal
ext 186637/s -- -19%
normal 230310/s 23% --
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>