How do I Capitalize the first letter?

Discussion in 'Perl Misc' started by nntp, Oct 8, 2004.

  1. nntp

    nntp Guest

    s/\s(\w)//g;
    how to inclue the fist letter in too?

    I want to make
    abc xyz to Abc Xyz
     
    nntp, Oct 8, 2004
    #1
    1. Advertising

  2. nntp

    Jim Keenan Guest

    nntp wrote:
    > s/\s(\w)//g;
    > how to inclue the fist letter in too?
    >
    > I want to make
    > abc xyz to Abc Xyz
    >
    >

    perldoc -f ucfirst
     
    Jim Keenan, Oct 8, 2004
    #2
    1. Advertising

  3. nntp <> wrote:

    > I want to make
    > abc xyz to Abc Xyz



    s/(\w+)/\u\L$1/g; # untested


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 9, 2004
    #3
  4. nntp

    Paul Lalli Guest

    Jim Keenan wrote:

    > nntp wrote:
    >
    >> s/\s(\w)//g;
    >> how to inclue the fist letter in too?
    >>
    >> I want to make
    >> abc xyz to Abc Xyz
    >>
    >>

    > perldoc -f ucfirst


    Have you yourself read that documentation? It doesn't do what the OP
    asked for.

    Paul Lalli
     
    Paul Lalli, Oct 9, 2004
    #4
  5. nntp

    Paul Lalli Guest

    nntp wrote:

    > s/\s(\w)//g;

    this is elminating the space and word character. That doesn't at all
    match your subject.
    > how to inclue the fist letter in too?
    >
    > I want to make
    > abc xyz to Abc Xyz
    >
    >

    \s is searching for a space. You just want to find the first character
    in each 'word'. Assuming you don't mind Perl's default definition of
    'word' characters (which include digits and underscores), you're going
    to want something closer to this:

    s/\b(\w)/\u$1/g;

    read perldoc perlre for info on \b. Read perldoc -f ucfirst for info on \u

    HOWEVER, this will not function as you intend for several phrases. One
    obvious example is:
    "this won't work", which would become "This Won'T Work".

    So perhaps you might actually want to look for the space, but also
    inclue the very first character in the string:

    s/(^|\s)(\w)/$1\u$2/g;

    Paul Lalli
     
    Paul Lalli, Oct 9, 2004
    #5
  6. nntp

    Ben Morrow Guest

    Quoth Paul Lalli <>:
    >
    > s/(^|\s)(\w)/$1\u$2/g;


    I would use positive look-behind:

    s/(?:^|(?<=\s))(\w)/\u$1/g;

    just 'cos it's neater. No variable-length lookbehind is annoying, though...

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Oct 9, 2004
    #6
  7. l v wrote:
    > nntp wrote:
    >>
    >> s/\s(\w)//g;
    >> how to inclue the fist letter in too?
    >>
    >> I want to make
    >> abc xyz to Abc Xyz

    >
    > Not sure on exactly what you are asking for. Below will capitalize the
    > first letter of every word in $a.
    >
    > join ' ', ( map {ucfirst $_ } split /\W/, $a );


    You are changing all the \W characters to ' ' which may not be what the OP
    wants. You could fix that by:

    join '', map ucfirst, split /(\W+)/, $a;


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 10, 2004
    #7
  8. nntp

    Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > Jim Keenan wrote:
    >
    > > nntp wrote:
    > >
    > >> s/\s(\w)//g;
    > >> how to inclue the fist letter in too?
    > >>
    > >> I want to make
    > >> abc xyz to Abc Xyz
    > >>
    > >>

    > > perldoc -f ucfirst

    >
    > Have you yourself read that documentation? It doesn't do what the OP
    > asked for.


    It does point to the "\u" escape, which is what the OP wants:

    s/\s(\w+)/ \u$1/g;

    or better

    s/\b(\w+)/\u$1/g;

    Anno
     
    Anno Siegel, Oct 11, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jay
    Replies:
    1
    Views:
    19,100
    Marc Dzaebel
    Feb 25, 2004
  2. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    774
    Reinhold Birkenfeld
    Jul 6, 2004
  3. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    511
    Tony Meyer
    Jul 6, 2004
  4. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    761
    Scott David Daniels
    Jul 6, 2004
  5. Replies:
    3
    Views:
    166
    Dr John Stockton
    Jun 5, 2005
Loading...

Share This Page