Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Elsewhere I saw a question asking about matching alternating case strings. Here's the regex, and a test script.
m/^([a-z]([A-Z][a-z])*[A-Z]?|[A-Z]([a-z][A-Z])*[a-z]?)$/

It looks a bit clunky, but it works.

Here's the test script:

#!/usr/bin/env perl # Test for alternating case regex use strict; use warnings; # Generate all lower/upper combos up to 5 characters with the characte +rs A-E my @test_strings = qw( a A ab aB Ab AB abc aBc Abc ABc abC aBC AbC ABC abcd aBcd Abcd ABcd abCd aBCd AbCd ABCd abcD aBcD AbcD ABcD abCD aBCD AbCD ABCD abcde aBcde Abcde ABcde abCde aBCde AbCde ABCde abcDe aBcDe AbcDe ABcDe abCDe aBCDe AbCDe ABCDe abcdE aBcdE AbcdE ABcdE abCdE aBCdE AbCdE ABCdE abcDE aBcDE AbcDE ABcDE abCDE aBCDE AbCDE ABCDE ); # test all strings, save them in a hash for sorting later my %results; for my $test (@test_strings) { my $result = $test =~ m/^([a-z]([A-Z][a-z])*[A-Z]?|[A-Z]([a-z][A-Z +])*[a-z]?)$/; $results{$test} = $result; } # print the results, sorted by result for my $key (sort by_result_or_length_or_alpha keys %results) { printf "%10s %d\n", $key, $results{$key}; } exit; sub by_result_or_length_or_alpha { ($results{$b} <=> $results{$a}) or (length($a) <=> length($b)) or ($b cmp $a) }

Here's the output:

a 1 A 1 aB 1 Ab 1 aBc 1 AbC 1 aBcD 1 AbCd 1 aBcDe 1 AbCdE 1 ab 0 AB 0 abc 0 abC 0 aBC 0 Abc 0 ABc 0 ABC 0 abcd 0 abcD 0 abCd 0 abCD 0 aBcd 0 aBCd 0 aBCD 0 Abcd 0 AbcD 0 AbCD 0 ABcd 0 ABcD 0 ABCd 0 ABCD 0 abcde 0 abcdE 0 abcDe 0 abcDE 0 abCde 0 abCdE 0 abCDe 0 abCDE 0 aBcde 0 aBcdE 0 aBcDE 0 aBCde 0 aBCdE 0 aBCDe 0 aBCDE 0 Abcde 0 AbcdE 0 AbcDe 0 AbcDE 0 AbCde 0 AbCDe 0 AbCDE 0 ABcde 0 ABcdE 0 ABcDe 0 ABcDE 0 ABCde 0 ABCdE 0 ABCDe 0 ABCDE 0

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Alternating Case Regex by QM

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found