Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Capitalize First Letter of Each Word

by fishbot_v2 (Chaplain)
on Jun 30, 2005 at 13:54 UTC ( [id://471316]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Capitalize First Letter of Each Word
in thread Capitalize First Letter of Each Word

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;

Replies are listed 'Best First'.
Re^4: Capitalize First Letter of Each Word
by holli (Abbot) on Jun 30, 2005 at 14:14 UTC
    s#\b(\w)(\w* ?)#\U$1\L$2#g
    does the trick.


    holli, /regexed monk/
      That finishing space is completely extraneous... the \b at the front basically is as well. Space is not a word char, so will be just fine if left alone. The \w at the beginning is basically implying \b, since it will only start to match where a word char is.

                      - Ant
                      - Some of my best work - (1 2 3)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found