in reply to Capitalization Case Help!

Now that you've gotten the necessary lectures about why you shouldn't be doing this, here is a way to do specifically what you asked for (even though I think the insertion of the space is a mistake more often then not, so I would be inclined to leave out the added space):
sub cap { local $_ = shift || ''; s/\b(\w\S*)/\u\L$1/g; s/(?<=\bO')(\w)/\u$1/g; s/(?<=\bMc)(\w)/ \u$1/g; return $_; }
Since that uses zero-width look-behind, which has to involve a fixed number of characters to match, you'd have to add another separate regex to handle the case of "Mac..." (that is, just adding "a?" to the last regex above would cause a run-time error).

Replies are listed 'Best First'.
Re^2: Capitalization Case Help!
by AnomalousMonk (Archbishop) on Sep 16, 2010 at 03:22 UTC
    ... zero-width look-behind ... has to involve a fixed number of characters to match ...

    5.10 adds the  \K variable-width look-behind Special Escape (see  "(?<=pattern)" "\K" in the Look Around Assertions section of perlre):

    >perl -wMstrict -le "print qq{ver $]}; for (@ARGV) { my $name = $_; $name =~ s{ \b Ma?c \K (\w) }{ \u$1}xmsg; print qq{'$_' -> '$name'}; } " Mcdonald Macdonald ver 5.010001 'Mcdonald' -> 'Mc Donald' 'Macdonald' -> 'Mac Donald'