search substring in a large string

Discussion in 'Perl Misc' started by Paul, Dec 9, 2005.

  1. Paul

    Paul Guest

    hi, greetings,

    I want to get the substring with the same prefix in a large string
    (about 6K size ). like this,


    $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";

    $pos1=index($mystring,"abcdefgh");

    $substring=substr($mystring,$pos1,16);


    In this way, I can get the first 16bit substring after "abcdefgh", the
    questions is How can I get the 2nd, 3rd substring after "abcdefgh"?


    thanks.
    Paul, Dec 9, 2005
    #1
    1. Advertising

  2. Paul wrote:
    > I want to get the substring with the same prefix in a large string
    > (about 6K size ). like this,
    >
    >
    > $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";
    >
    > $pos1=index($mystring,"abcdefgh");
    >
    > $substring=substr($mystring,$pos1,16);
    >
    >
    > In this way, I can get the first 16bit substring


    What is a 16bit substring?

    > after "abcdefgh", the
    > questions is How can I get the 2nd, 3rd substring after "abcdefgh"?


    Just an idea: why not split() the string at 'abcdefgh' an then grab whatever
    you need from each element of the returned list?

    jue
    Jürgen Exner, Dec 9, 2005
    #2
    1. Advertising

  3. Paul wrote:
    > I want to get the substring with the same prefix in a large string
    > (about 6K size ). like this,
    >
    > $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";
    >
    > $pos1=index($mystring,"abcdefgh");
    >
    > $substring=substr($mystring,$pos1,16);
    >
    > In this way, I can get the first 16bit substring after "abcdefgh",


    Suppose you mean the first 16 characters substring beginning with
    "abcdefgh".

    > How can I get the 2nd, 3rd substring after "abcdefgh"?


    my ($pos, @substrings) = 0;
    while ( ( $pos = index $mystring, 'abcdefgh', $pos ) >= 0 ) {
    push @substrings, substr($mystring, $pos, 16);
    $pos += 16;
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 9, 2005
    #3
  4. Paul

    Anno Siegel Guest

    Paul <> wrote in comp.lang.perl.misc:
    > hi, greetings,
    >
    > I want to get the substring with the same prefix in a large string
    > (about 6K size ). like this,
    >
    >
    > $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345

    dafsdfe4q35345abcdefghhherwrtwert3245234";
    >
    > $pos1=index($mystring,"abcdefgh");
    >
    > $substring=substr($mystring,$pos1,16);
    >
    >
    > In this way, I can get the first 16bit substring after "abcdefgh", the


    You mean "16-byte substring".

    > questions is How can I get the 2nd, 3rd substring after "abcdefgh"?


    Read the documentation of index again and pay attention to the parameter
    named "POSITION".

    Or get them all at once:

    my @extract = $mystring =~ /abcdefgh(.{16})/sg;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 9, 2005
    #4
  5. Anno Siegel wrote:
    > Paul <> wrote in comp.lang.perl.misc:
    > >
    > > $substring=substr($mystring,$pos1,16);
    > >
    > > In this way, I can get the first 16bit substring after "abcdefgh", the

    >
    > You mean "16-byte substring".


    You mean "16-character substring" :)

    substr() operates on characters not bytes unless the "use bytes" pragma
    is in effect.
    Brian McCauley, Dec 10, 2005
    #5
  6. Paul wrote:
    > hi, greetings,
    >
    > I want to get the substring with the same prefix in a large string
    > (about 6K size ). like this,
    > $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";
    >
    > $pos1=index($mystring,"abcdefgh");
    >
    > $substring=substr($mystring,$pos1,16);
    >
    > In this way, I can get the first 16bit substring after "abcdefgh", the
    > questions is How can I get the 2nd, 3rd substring after "abcdefgh"?


    Although index()/substr() are often faster at runtime it may be more
    ideomatic to use a regex match.

    If you perfer to optomise for readability over speed and want to loop
    through all matches...

    while ( $mystring =~ /(abcdefgh.{8})/g ) {
    my $substring = $1;

    }
    Brian McCauley, Dec 10, 2005
    #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:
    3
    Views:
    393
    Roedy Green
    Dec 27, 2005
  2. Nico Grubert
    Replies:
    1
    Views:
    433
    Pierre Quentel
    Jun 21, 2006
  3. Timur Tabi
    Replies:
    4
    Views:
    893
    Timur Tabi
    Oct 14, 2009
  4. Replies:
    3
    Views:
    189
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    201
Loading...

Share This Page