help on split

Discussion in 'Perl' started by Hai Xu, Dec 7, 2003.

  1. Hai Xu

    Hai Xu Guest

    Hi,

    I ahve a string
    string="ThisAAaAAteAAAAst"
    now, I want to split it using "AA" as separator, say
    @str = split(/AA/, $string);
    however, I want to leave any AA+ as it is, that is,
    AAAA and any part of AAAA are not considered as
    separator.

    Any one knows how to use string pattern match to
    do it ?
    Of course, I can do it my own way, but I'd like to see
    if it's possible in pattern matching.

    Thanks

    Hai
     
    Hai Xu, Dec 7, 2003
    #1
    1. Advertising

  2. Hai Xu wrote:
    > I ahve a string
    > string="ThisAAaAAteAAAAst"
    > now, I want to split it using "AA" as separator, say
    > @str = split(/AA/, $string);
    > however, I want to leave any AA+ as it is, that is, AAAA and any
    > part of AAAA are not considered as separator.
    >
    > Any one knows how to use string pattern match to do it ?


    One way:

    @str = split /(?<!A)AA(?!A)/, $string;

    http://www.perldoc.com/perl5.8.0/pod/perlre.html#Extended-Patterns

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 7, 2003
    #2
    1. Advertising

  3. Hai Xu

    Hai Xu Guest

    Thank you very much

    Hai


    "Gunnar Hjalmarsson" <> wrote in message
    news:KtKAb.41123$...
    > Hai Xu wrote:
    > > I ahve a string
    > > string="ThisAAaAAteAAAAst"
    > > now, I want to split it using "AA" as separator, say
    > > @str = split(/AA/, $string);
    > > however, I want to leave any AA+ as it is, that is, AAAA and any
    > > part of AAAA are not considered as separator.
    > >
    > > Any one knows how to use string pattern match to do it ?

    >
    > One way:
    >
    > @str = split /(?<!A)AA(?!A)/, $string;
    >
    > http://www.perldoc.com/perl5.8.0/pod/perlre.html#Extended-Patterns
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >
     
    Hai Xu, Dec 7, 2003
    #3
    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. Replies:
    2
    Views:
    475
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    712
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    226
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    255
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    638
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page