blue_cowdawg has asked for the wisdom of the Perl Monks concerning the following question:

DADGUMMIT!

Once upon a time I used to use a regex that IIRC used tr to convert the case of strings on word boundaries and I just can't remember how I did it.

Example: I have a name that is in all caps "YOSEMITE SAM" and I want to convert it to "Yosemite Sam."

I remember doing this and it was really simple, but dadgummit if I can remember or figure it out again. The worst part of it is that I used it as an example in a class I taught and my lecture notes are not here they are at home.

Just about as bad as losing the formula for ice cubes.


Peter L. BergholdSchooner Technology Consulting, Inc.
Peter@Berghold.Netwww.berghold.net
Unix Professional Services

Replies are listed 'Best First'.
Re: Changing case on a word boundary
by zigdon (Deacon) on Oct 28, 2002 at 20:45 UTC
    Dunno about tr, but here's one way of doing this:
    my $str = "YOSEMITE SAM"; $str =~ s/(\w+)/\u\L$1/g;

    Note that \w+ might not match your definition of word, but you should be able to modify it to suit.

    -- Dan

      Thanks, Dan.

      I think I have gone from CRS to full blown CRAFT.

      It is very frustrating when you remember doing something and you can't remember how.

      Here is another solution (very similar to yours) that I was given over on the Chatterbox.

      	$str="YOSEMITE SAM";
      	$str=~ s/(A-Z+)/\L\u$1/g;
      

      The only difference of course being the definition of a " word" as you intimated in your post.


      Peter L. BergholdBrewer of Belgian Ales
      Peter@Berghold.Netwww.berghold.net
      Unix Professional
Re: Changing case on a word boundary
by BrowserUk (Patriarch) on Oct 28, 2002 at 20:50 UTC

    Try this.

    $s ='the quick brown fox'; $s =~ s/\b(\w+)\b/ucfirst $1/ge; print $s;

    Gives

    The Quick Brown Fox

    Update: zigdon++ pointed out that your question showed the input as ALL UPPER CASE, which means that you would need

     $s =~ s/\b(\w+)\b/ucfirst lc $1/ge;.

    Much safer.


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy