Is there a command or package to search for all instances of asubstring in a string?

Discussion in 'Perl Misc' started by Peng Yu, Nov 30, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    Can somebody let me know if there is a command or function to find all
    the occurance of a substring in a string? I can write a program to do
    this. I just want to know what is the best way to do this.

    Thanks,
    Peng
    Peng Yu, Nov 30, 2008
    #1
    1. Advertising

  2. Re: Is there a command or package to search for all instances ofa substring in a string?

    Peng Yu wrote:
    >
    > Can somebody let me know if there is a command or function to find all
    > the occurance of a substring in a string? I can write a program to do
    > this. I just want to know what is the best way to do this.


    perldoc -f m
    perldoc -f index
    perldoc -f rindex


    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 30, 2008
    #2
    1. Advertising

  3. Re: Is there a command or package to search for all instances of a substring in a string?

    Peng Yu <> wrote:

    > Can somebody let me know if there is a command or function to find all
    > the occurance of a substring in a string?



    What does "find" mean when you say it?

    If string="aaa" and substring="aa", should it "find" the substring 1 or 2 times?


    > I just want to know what is the best way to do this.



    That depends entirely on what "this" is...

    If "this" is:

    find all of the starting indexes of a substring in a larger string

    then to find non-overlapping substrings:

    push @indexes, pos($string) - length $substr, while $string =~ /$substr/g;

    or to find overlapping substrings:

    for (my $pos = -1;;) {
    $pos = index $string, $substr, $pos+1;
    last if $pos == -1;
    push @indexes, $pos;
    }


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Nov 30, 2008
    #3
  4. Re: Is there a command or package to search for all instances of a substring in a string?

    Peng Yu <> wrote:
    >Can somebody let me know if there is a command or function to find all
    >the occurance of a substring in a string? I can write a program to do
    >this. I just want to know what is the best way to do this.


    perldoc -f index

    and keep looking ( aka looping)

    jue
    Jürgen Exner, Nov 30, 2008
    #4
  5. Re: Is there a command or package to search for all instances of a substring in a string?

    [A complimentary Cc of this posting was sent to
    Tad J McClellan
    <>], who wrote in article <>:
    > find all of the starting indexes of a substring in a larger string
    >
    > then to find non-overlapping substrings:
    >
    > push @indexes, pos($string) - length $substr, while $string =~ /$substr/g;


    One should keep in mind that overlapping substrings can be done in the
    same way:

    push @indexes, pos($string) - length $substr, while $string =~ /(?=$substr)/g;

    # As above, $substr should not contain metachars; otherwise \Q\E may
    be needed...

    Hope this helps,
    Ilya
    Ilya Zakharevich, Nov 30, 2008
    #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. RC
    Replies:
    1
    Views:
    654
    Tor Iver Wilhelmsen
    Jul 7, 2005
  2. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  3. Replies:
    8
    Views:
    459
    James Stroud
    Jan 29, 2009
  4. Laphan
    Replies:
    1
    Views:
    103
    Anthony Jones
    Jun 18, 2006
  5. haomiao
    Replies:
    2
    Views:
    103
    haomiao
    Jul 25, 2007
Loading...

Share This Page