Very clever, -10. (I got that on an assignment once.)

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.

#!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"; }
Update: had replaced .* with +, when it can simply be removed altogether.

In reply to Re: Re: Regexp for alphbetical order match within the string by Roy Johnson
in thread Regexp for alphabetical order match within the string by prostoalex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.