I'm guessing that u want to cannocalize the capitalization of string words
right?  Like BOB JONES -> Bob Jones.  There is a faster way to check for
mixed casedness.

%tolowers = map {$_, 1} ('A', 'The', 'If', 'Is', 'It', 'Of', 'Our',
'An','On', 'In', 'But', 'With', 'Has', 'Had', 'Have');
%touppers = map {$_, 1} ('N','S','E','W','NE','NW','SE','SW','PO','BOX');
$uppers = $text =~ tr/A-Z/A-Z/; #count uppercase letters
$lowers = $text =~ tr/a-z/a-z/; #count lowercase letters

if ($uppers and not $lowers) { #all upper case
        &fixcase($text);
}
elsif ($lowers and not $uppers) { #all lower case
        &fixcase($text);
}

sub fixcase {
        my $text = $_[0];
        my @text = map {ucfirst(lc($_))} split / /, $text;
        foreach $i (@text) { $tolowers{$i} and $i = lc $i; }
        foreach $i (@text) { $touppers{uc $i} and $i = uc $i; }
        $text = join " ", @text;
        # do whatever else
        return $text;
}

That should do it and be about as efficient as possible. :)  If u have to
deal with sentences then u'll need a few more lines to deal with periods and
commas.

These O'Reilly gems are useful too.
Finding all-caps words 
    @capwords = m/(\b[^\Wa-z0-9_]+\b)/g;
Finding all-lowercase words 
    @lowords = m/(\b[^\WA-Z0-9_]+\b)/g;
Finding initial-caps word 
    @icwords = m/(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)/;

At 12:12 PM 2/28/2007 -0500, Chris O wrote:
>In the sample below, I'm checking $foo for all caps or all lowercase. Is
>there a more efficient regex method?
>
>$foo='APPLE JONES PARKER';
>
>if(($foo!~/[A-Z]/)or($foo!~/[a-z]/)){
>       $foo=&title_case($foo);
>}
>
>print $foo."\n";
>
>sub title_case{
>  my($string) = @_;
>  my @exception_words = ('A', 'The', 'If', 'Is', 'It', 'Of', 'Our',
>'An','On', 'In', 'But', 'With', 'Has', 'Had', 'Have');
>  my @exception_stuff = ('N','S','E','W','NE','NW','SE','SW','PO','BOX');
>  
>  $string =~ s/([\w']+)/\u\L$1/g;
>  foreach(@exception_words){$string =~ s/\b$_\b/lc($_)/ge;} # Make Exception
>Words LC
>  foreach(@exception_stuff){$string =~ s/\b$_\b/$_/gei;} # Make Exception
>Stuff Correct Case
>  
>  $string =~ s/(.)/\u$1/; # Uppercase the first letter
>  return $string;
>}



--
REMEMBER THE WORLD TRADE CENTER         ---=< WTC 911 >=--
"...ne cede malis"

00000100

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to