Stone [S], on Thursday, March 10, 2005 at 16:46 (-0800) typed:
S> That won't work either. When you say ([1-9]\d?) you're telling it
S> "(If there is a match) capture the stuff in parentheses and store it."
S> When you say "\1" you're telling the script "you know that first
S> batch of stuff you captured? Well I want that here." But it's not
S> the pattern, it's the literal substring of the match.
When I read your first line of reply, I realise, that you have right
again. Tomorrow was hard day for me, and also after some glassess of
wine I simply forget to some small details. And also I have to thank
you for nice and long explanation :) Ok, so I'm sorry, if I
confused someone, here is my explanation:
I don't like "simple" regular expressions, as was posted in first
message:
$field =~ /^[1-9]\d?\.[1-9]\d?\.[1-9]\d?$/;
that's too clear. I wanted change it (short), so I post 2 bad
examples. Clear shorting is:
$field =~ /^([1-9]\d?\.){2}[1-9]\d?$/;
but that's too much simple too :)
So let's continue how we can represent those rulez, this is a bit
tricky:
$field =~ /^([1-9]\d(?:\.[1-9]\d){2})$/;
and this is not shorter, but this way works too :):
$field =~ /^(?:(\d)(??{ $1 > 0 && $1 <= 9 ? "" : "(?!)" })\d\.){2}
(\d)(??{ $2 > 0 && $2 <= 9 ? "" : "(?!)" })\d/x;
Ok, that's enough, now we can benchmark all those:
use strict;
use warnings;
use Benchmark 'cmpthese';
my $field = '19.12.12';
cmpthese -5, {
simple => sub {
if ( $field =~ /^[1-9]\d?\.[1-9]\d?\.[1-9]\d?$/ ) {}
},
short => sub {
if ( $field =~ /^([1-9]\d\.){2}[1-9]\d$/ ) {}
},
shortwoc => sub {
if ( $field =~ /^(?:[1-9]\d\.){2}[1-9]\d$/ ) {}
},
tricky => sub {
if ( $field =~ /^([1-9]\d(?:\.[1-9]\d){2})$/ ) {}
},
trickywoc => sub {
if ( $field =~ /^(?:[1-9]\d(?:\.[1-9]\d){2})$/ ) {}
},
long => sub {
if ( $field =~ /^(?:(\d)(??{ $1 > 0 && $1 <= 9 ? "" : "(?!)"
})\d\.){2}
(\d)(??{ $2 > 0 && $2 <= 9 ? "" : "(?!)"
})\d/x ) {}
},
using_var => sub {
my $reg = '[1-9]\d';
if ( $field =~ /^(?:$reg\.){2}$reg$/ ) {}
}
};
__END__
Rate long using_var short tricky simple shortwoc trickywoc
long 30584/s -- -95% -95% -95% -98% -98% -98%
using_var 582064/s 1803% -- -11% -14% -56% -60% -62%
short 650382/s 2027% 12% -- -3% -51% -56% -58%
tricky 673134/s 2101% 16% 3% -- -49% -54% -57%
simple 1328337/s 4243% 128% 104% 97% -- -10% -14%
shortwoc 1470620/s 4708% 153% 126% 118% 11% -- -5%
trickywoc 1550263/s 4969% 166% 138% 130% 17% 5% --
second benchmark:
my $field = '19.12.02';
Rate long using_var shortwoc short simple tricky trickywoc
long 28809/s -- -95% -98% -98% -98% -98% -98%
using_var 606756/s 2006% -- -63% -63% -64% -64% -66%
shortwoc 1627591/s 5550% 168% -- -1% -3% -5% -9%
short 1651154/s 5631% 172% 1% -- -2% -3% -8%
simple 1681601/s 5737% 177% 3% 2% -- -2% -6%
tricky 1707274/s 5826% 181% 5% 3% 2% -- -5%
trickywoc 1790738/s 6116% 195% 10% 8% 6% 5% --
so conlusion is: I definitely would pick up
tricky WithOut Capturing: $field =~ /^(?:[1-9]\d(?:\.[1-9]\d){2})$/
I hope I don't have any bugs here :)
Have a nice day.
--
...m8s, cu l8r, Brano.
["Real knowledge is to know the extent of one's ignorance." - Confucius]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>