in reply to Re: Regexp for alphabetical order match within the string
in thread Regexp for alphabetical order match within the string
Surprisingly, your expression is not case-insensitive, despite the /i switch. That is because you can end up with character classes like [B-z]. One possible remedy: /(.)(??{"[^lc($1)-z]"})/. (Also note that you don't need the .*).
The other thing about your regex is that it is about 30 times slower than the other recommendations. Don't even try to benchmark it for more than a few thousand iterations. But it is cool.
The other solutions are all comparable to each other for performance. My benchmarking code attached.
Update: had replaced .* with +, when it can simply be removed altogether.#!perl use strict; use warnings; my @tstrs = qw( abdc aBcxz abccCcccz abcdefghijklmnop qrstuvwxyzabz zabc abca ); sub m_sorted { $_[0] !~ /(.)(??{"[^lc($1)-z]"})/; } sub substr_sorted { my $lstr = lc shift; my $test = substr($lstr, 0, 1); for my $i (1..length($lstr)-1) { my $ntst = substr($lstr, $i, 1); return 0 if $ntst lt $test; $test = $ntst; } 1; } sub expl_sorted { my $lstr = lc shift; $lstr eq join('', sort split //, $lstr); } my $gen_m_pat = join '', map "$_*", 'a'..'z'; sub gen_m_sorted { $_[0] =~ /^$gen_m_pat$/oi; } use Benchmark; timethese(100_000, { # 'm_sorted' => sub { for (@tstrs) { chomp; my $is = m_sorted +$_; } }, 'substr_sorted' => sub { for (@tstrs) { chomp; my $is = substr_sor +ted $_; }}, 'expl_sorted' => sub { for (@tstrs) { chomp; my $is = expl_sorte +d $_; }}, 'gen_m' => sub { for (@tstrs) { chomp; my $is = gen_m_sorted $_; + }}, }); for (@tstrs) { print "$_\n"; print " m says: ", m_sorted($_) ? '' : 'not ', "sorted\n"; print " substr says: ", substr_sorted($_) ? '' : 'not ', "sorted\n +"; print " expl says: ", expl_sorted($_) ? '' : 'not ', "sorted\n"; print " gen_m says: ", gen_m_sorted($_) ? '' : 'not ', "sorted\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Regexp for alphbetical order match within the string
by BrowserUk (Patriarch) on Oct 30, 2003 at 22:33 UTC | |
by Roy Johnson (Monsignor) on Oct 31, 2003 at 14:47 UTC |