using split to extract characters

Discussion in 'Perl Misc' started by zenny lenny, May 1, 2007.

  1. zenny lenny

    zenny lenny Guest

    This should be relatively simple but I can't figure it out. I need to
    extract the center 3 characters from this data. The text xyz and abc
    do not change:

    xyz3r4abc
    xyzm9zabc
    xyzuukabc

    I tried several variations on this command, using xyz and abc as
    delimiters. The problem is, if any of the characters x,y,z,a,b,c
    appear in the data they will be treated as delimiters, so this won't
    work.

    ($data)=split /[xyz,abc]+/

    Is there any way to treat xyz and abc as strings? I tried double and
    single quotes but that didn't work.

    tia
     
    zenny lenny, May 1, 2007
    #1
    1. Advertising

  2. zenny lenny

    Mirco Wahab Guest

    zenny lenny wrote:
    > This should be relatively simple but I can't figure it out. I need to
    > extract the center 3 characters from this data. The text xyz and abc
    > do not change:
    >
    > xyz3r4abc
    > xyzm9zabc
    > xyzuukabc
    >
    > I tried several variations on this command, using xyz and abc as
    > delimiters. The problem is, if any of the characters x,y,z,a,b,c
    > appear in the data they will be treated as delimiters, so this won't
    > work.
    >
    > ($data)=split /[xyz,abc]+/
    >
    > Is there any way to treat xyz and abc as strings? I tried double and
    > single quotes but that didn't work.


    try to extract it by matching a regular
    expression on it, like /(?<=xyz)...(?=abc)/

    example:

    ...
    my @data = qw'
    xyz3r4abc
    xyzm9zabc
    xyzuukabc
    ';

    @data = map /(?<=xyz)...(?=abc)/g, @data;

    print "@data";


    Regards

    M.
     
    Mirco Wahab, May 1, 2007
    #2
    1. Advertising

  3. zenny lenny

    Mirco Wahab Guest

    Mirco Wahab wrote:
    > zenny lenny wrote:
    >> This should be relatively simple but I can't figure it out. I need to
    >> extract the center 3 characters from this data. The text xyz and abc
    >> do not change:
    >> xyz3r4abc
    >> xyzm9zabc
    >> xyzuukabc

    > try to extract it by matching a regular
    > expression on it, like /(?<=xyz)...(?=abc)/


    Oh, if you want 'split', then you could use
    the | (or) pattern alternation operator:

    ...
    my @data = qw'
    xyz3r4abc
    xyzm9zabc
    xyzuukabc
    ';

    print split /xyz|abc/ for @data;



    Regards

    M.
     
    Mirco Wahab, May 1, 2007
    #3
  4. zenny lenny

    zenny lenny Guest

    On May 1, 9:20 am, Glenn Jackman <> wrote:
    > At 2007-05-01 11:59AM, "zenny lenny" wrote:
    >
    >
    >
    > > This should be relatively simple but I can't figure it out. I need to
    > > extract the center 3 characters from this data. The text xyz and abc
    > > do not change:

    >
    > > xyz3r4abc
    > > xyzm9zabc
    > > xyzuukabc

    >
    > > I tried several variations on this command, using xyz and abc as
    > > delimiters. The problem is, if any of the characters x,y,z,a,b,c
    > > appear in the data they will be treated as delimiters, so this won't
    > > work.

    >
    > > ($data)=split /[xyz,abc]+/

    >
    > > Is there any way to treat xyz and abc as strings? I tried double and
    > > single quotes but that didn't work.

    >
    > Using split: ($data) = split /xyz|abc/;
    >
    > But why use split? If you know you need a substring, use substr:
    > $data = substr $_, 3, 3;
    >
    > --
    > Glenn Jackman
    > "You can only be young once. But you can always be immature." -- Dave Barry



    Perfect! Thank you!
     
    zenny lenny, May 1, 2007
    #4
  5. zenny lenny wrote:
    > This should be relatively simple but I can't figure it out. I need to
    > extract the center 3 characters from this data. The text xyz and abc
    > do not change:
    >
    > xyz3r4abc
    > xyzm9zabc
    > xyzuukabc
    >
    > I tried several variations on this command, using xyz and abc as
    > delimiters. The problem is, if any of the characters x,y,z,a,b,c
    > appear in the data they will be treated as delimiters, so this won't
    > work.
    >
    > ($data)=split /[xyz,abc]+/


    When it comes to REs people seem to get so excited that they loose their
    common sense. Why on earth do you want to wield the big and certainly
    powerful RE gun when a very modest and simple substr() does the job
    perfectly fine?

    $data = substr ($_, 3, 3);

    jue
     
    Jürgen Exner, May 1, 2007
    #5
  6. zenny lenny <> wrote:
    > This should be relatively simple but I can't figure it out. I need to
    > extract the center 3 characters from this data. The text xyz and abc
    > do not change:


    > xyz3r4abc
    > xyzm9zabc
    > xyzuukabc


    > I tried several variations on this command, using xyz and abc as
    > delimiters. The problem is, if any of the characters x,y,z,a,b,c
    > appear in the data they will be treated as delimiters, so this won't
    > work.


    > ($data)=split /[xyz,abc]+/


    > Is there any way to treat xyz and abc as strings? I tried double and
    > single quotes but that didn't work.


    Why don't you use e.g.

    ( $data = $_ ) =~ s/^xyz(.*?)abc$/$1/;

    (assuming that the text is stored in '$_')? Or, if it's always
    three characters in the middle with three in front of them

    $data = substr $_, 3, 3;

    If you insist on using split try

    $data = ( split /^xyz|abc$/ )[ 1 ];

    The '^' and '$' are important since they ensure that the match
    on 'xyz' only happens at the very start and that on 'abc' only
    at the very end, so it will also work with text like "xyzxyzabc"
    or "xyzabcabc".
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 1, 2007
    #6
    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:
    492
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    742
    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:
    240
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    278
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    713
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page