String Manipulation

Discussion in 'Perl Misc' started by dao@snakebrook.com, Nov 12, 2006.

  1. Guest

    Hi, I am kina a perl-newbie and have both the learning perl book an the
    programming perl book, but can't find the information I'm looking for
    in either of them. What I need to do is as follows:

    I have a string: AaaSmmmRiiiW

    And I need to recognize te capital letters and turn the string into the
    following: Aaa Smmm Riii W

    Basically, every Capital needs to be preceeded by a space. I can't
    figure out how to do it in perl.

    I lloked at the FAQ at perl.org and came up empty there as well. I know
    it can be done, I just can't figure it out.

    Any help appreciated.


    Thanks, Jack
     
    , Nov 12, 2006
    #1
    1. Advertising

  2. Dr.Ruud Guest

    Mirco Wahab schreef:
    > dao:


    >> I have a string: AaaSmmmRiiiW
    >>
    >> And I need to recognize te capital letters and turn the string into
    >> the following: Aaa Smmm Riii W
    >>
    >> Basically, every Capital needs to be preceeded by a space. I can't
    >> figure out how to do it in perl.

    >
    >
    > You could use a regular expression substitution:
    >
    > my $str = 'AaaSmmmRiiiW';
    >
    > $str =~ s/(?<!^)([A-Z])/ $1/g;


    Variant without capturing:

    perl -wle'
    $_="AaaSmmmRiiiW";

    s/(?<!^)(?=[A-Z])/ /g;

    print
    '

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 13, 2006
    #2
    1. Advertising

  3. wrote:
    >
    > Hi, I am kina a perl-newbie and have both the learning perl book an the
    > programming perl book, but can't find the information I'm looking for
    > in either of them. What I need to do is as follows:
    >
    > I have a string: AaaSmmmRiiiW
    >
    > And I need to recognize te capital letters and turn the string into the
    > following: Aaa Smmm Riii W
    >
    > Basically, every Capital needs to be preceeded by a space. I can't
    > figure out how to do it in perl.
    >
    > I lloked at the FAQ at perl.org and came up empty there as well. I know
    > it can be done, I just can't figure it out.
    >
    > Any help appreciated.


    $ perl -le'$_ = q[AaaSmmmRiiiW]; print; s/(?=[[:upper:]])(?!\A)/ /g; print;'
    AaaSmmmRiiiW
    Aaa Smmm Riii W




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Nov 13, 2006
    #3
  4. <> wrote:

    > I have a string: AaaSmmmRiiiW
    >
    > And I need to recognize te capital letters and turn the string into the
    > following: Aaa Smmm Riii W
    >
    > Basically, every Capital needs to be preceeded by a space.



    Or, every run of lower case chars need to be succeeded by a space:


    s/([a-z]+)/$1 /g;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 13, 2006
    #4
  5. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    >
    >
    > Hi, I am kina a perl-newbie and have both the learning perl book an the
    > programming perl book, but can't find the information I'm looking for
    > in either of them. What I need to do is as follows:
    >
    > I have a string: AaaSmmmRiiiW
    >
    > And I need to recognize te capital letters and turn the string into the
    > following: Aaa Smmm Riii W
    >
    > Basically, every Capital needs to be preceeded by a space. I can't
    > figure out how to do it in perl.
    >
    > I lloked at the FAQ at perl.org and came up empty there as well. I know
    > it can be done, I just can't figure it out.
    >
    > Any help appreciated.
    >
    >
    > Thanks, Jack


    ....or split the string before each capital, then join it together with
    blanks:

    join ' ', split /(?=[[:upper:]])/;

    Anno
     
    -berlin.de, Nov 13, 2006
    #5
    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. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    815
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,828
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,647
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    834
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    455
    Pete Wright
    May 24, 2004
Loading...

Share This Page