How to find every occurrence of a substring by a function?

Discussion in 'Perl Misc' started by Peng Yu, Jan 25, 2010.

  1. Peng Yu

    Peng Yu Guest

    Peng Yu, Jan 25, 2010
    #1
    1. Advertising

  2. Peng Yu wrote:
    > Example 3b on the following page use a loop to find all substring.
    >
    > http://perlmeme.org/howtos/perlfunc/index_function.html
    >
    > I'm wondering if there is a function in perl library that can give me
    > back all the matches, just like findall in python (see section 3.3 in
    > the following page).
    >
    > http://www.amk.ca/python/howto/regex/


    Sure:

    my @findall = $string =~ /pattern/g;




    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
     
    John W. Krahn, Jan 25, 2010
    #2
    1. Advertising

  3. Peng Yu <> wrote:
    >Example 3b on the following page use a loop to find all substring.
    >
    >http://perlmeme.org/howtos/perlfunc/index_function.html
    >
    >I'm wondering if there is a function in perl library that can give me
    >back all the matches,


    Maybe I misunderstand what you are looking for but it appears to me that
    the g-modifier in list context does exactly what you want. From 'perldoc
    perlretut':

    In list context, "//g" returns a list of matched groupings, or
    if there are no groupings, a list of matches to the whole
    regexp.

    jue
     
    Jürgen Exner, Jan 25, 2010
    #3
  4. Peng Yu

    Peng Yu Guest

    On Jan 24, 8:49 pm, "John W. Krahn" <> wrote:
    > Peng Yu wrote:
    > > Example 3b on the following page use a loop to find all substring.

    >
    > >http://perlmeme.org/howtos/perlfunc/index_function.html

    >
    > > I'm wondering if there is a function in perl library that can give me
    > > back all the matches, just like findall in python (see section 3.3 in
    > > the following page).

    >
    > >http://www.amk.ca/python/howto/regex/

    >
    > Sure:
    >
    > my @findall = $string =~ /pattern/g;


    I think that I didn't make it clear. I want the location of the match.

    For example, for the following variables, I want get an array that has
    the number 0 and 7. You command gives an array of two same strings
    'abc' and 'abc'.

    $string="abcdefgabc";
    $substring="abc";
     
    Peng Yu, Jan 25, 2010
    #4
  5. Peng Yu wrote:
    > On Jan 24, 8:49 pm, "John W. Krahn" <> wrote:
    >> Peng Yu wrote:
    >>> Example 3b on the following page use a loop to find all substring.
    >>> http://perlmeme.org/howtos/perlfunc/index_function.html
    >>> I'm wondering if there is a function in perl library that can give me
    >>> back all the matches, just like findall in python (see section 3.3 in
    >>> the following page).
    >>> http://www.amk.ca/python/howto/regex/

    >> Sure:
    >>
    >> my @findall = $string =~ /pattern/g;

    >
    > I think that I didn't make it clear. I want the location of the match.


    You said that you wanted something "just like findall" which according
    to the page you referenced "Find all substrings where the RE matches,
    and returns them as a list." which is exactly what the Perl code above does.


    > For example, for the following variables, I want get an array that has
    > the number 0 and 7. You command gives an array of two same strings
    > 'abc' and 'abc'.
    >
    > $string="abcdefgabc";
    > $substring="abc";


    $ perl -le'
    my $string = "abcdefgabc";
    my $substring = "abc";
    my @offsets;
    { use re "eval";
    $string =~ /\Q$substring\E(??{ push @offsets, $-[ 0 ] })/g;
    }
    print "@offsets";
    '
    0 7




    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
     
    John W. Krahn, Jan 25, 2010
    #5
  6. Peng Yu <> wrote:
    >On Jan 24, 8:49 pm, "John W. Krahn" <> wrote:
    >> Peng Yu wrote:
    >> > I'm wondering if there is a function in perl library that can give me
    >> > back all the matches, just like findall in python (see section 3.3 in
    >> > the following page).
    >> >http://www.amk.ca/python/howto/regex/

    >>
    >> my @findall = $string =~ /pattern/g;

    >
    >I think that I didn't make it clear. I want the location of the match.


    You wrote: "give me back all the matches". That is different from the
    location of those matches.

    And according to the documentation your Python findall() appears to
    return the found matches, too, not their positions:

    findall() Find all substrings where the RE matches, and
    returns them as a list

    jue
     
    Jürgen Exner, Jan 25, 2010
    #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. Registered User

    Find first occurrence of string and replace

    Registered User, Oct 16, 2006, in forum: C Programming
    Replies:
    6
    Views:
    501
    Ed Collins
    Oct 18, 2006
  2. Xernoth
    Replies:
    6
    Views:
    365
    Jerry Coffin
    Apr 21, 2007
  3. Jack
    Replies:
    8
    Views:
    159
  4. C3
    Replies:
    3
    Views:
    133
    Brian McCauley
    Sep 29, 2004
  5. Replies:
    3
    Views:
    235
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page