Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Capitalize First Letter of Each Word

by gube (Parson)
on Jun 30, 2005 at 13:20 UTC ( [id://471302]=note: print w/replies, xml ) Need Help??


in reply to Capitalize First Letter of Each Word

@a = ("THIS IS TESTING", "JOE MARCONES", "RESIDENTIAL MAINTENANCE COMPANY", ); s#(.)(\w+)( )?#\U\1\L\2\3#gsi for @a; print join "\n", @a;

Replies are listed 'Best First'.
Re^2: Capitalize First Letter of Each Word
by holli (Abbot) on Jun 30, 2005 at 13:26 UTC
    2 points.

    1. It is save to use the ? without braces if the optional part is a single char.
    2. No need for the i and s modifier

    So that boils down to
    s#(.)(\w+ ?)#\U\1\L\2#g for @a;
    which looks actually nicer than my original post.


    holli, /regexed monk/

      It does, except it doesn't work:

      my @a = ("a sTrinG\n" ); s#(.)(\w+ ?)#\U\1\L\2#g for @a; print @a; __END__ a string

      Two things are happening here: The first is that this requires each word to be two characters or more (so it fails on the initial "a"). The second is that the . can match a space, so " sTrinG" fails because \1 contains a space.

      I suggest

      s{ \b(\w+) }{\u\L$1}gx;
        s#\b(\w)(\w* ?)#\U$1\L$2#g
        does the trick.


        holli, /regexed monk/
      I just need to trough this here as well.
      What if I have some uncials like, "LTD" at the end of the string or on the string and it couldn't be changed
        I don't get you. What on earth is a "uncial"?


        holli, /regexed monk/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://471302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found