Find location of substring in string?

Discussion in 'Perl Misc' started by fishfry, Sep 24, 2004.

  1. fishfry

    fishfry Guest

    I can't remember the function that, given "abcdef" and "de" returns 3,
    indicating the character position, starting from 0, where "de" is found
    in "abcdef".

    A secondary question is, how do I use perldoc and the other online docs
    when I can't remember the name of the function I'm looking for? For
    example I remembered substr(), but that goes the other way, returning
    the string occurring at a particular character position.

    And my third question is, how can I do this as part of a pattern match?
    For example I want to do something like

    if ($foo =~ /\d\d/) {
    # do stuff
    }

    where what I really want to know is WHERE within $foo, a pair of digits
    occurred.

    Thank you all kindly in advance.
     
    fishfry, Sep 24, 2004
    #1
    1. Advertising

  2. fishfry

    Sam Holden Guest

    On Fri, 24 Sep 2004 02:19:22 GMT,
    fishfry <> wrote:
    > I can't remember the function that, given "abcdef" and "de" returns 3,
    > indicating the character position, starting from 0, where "de" is found
    > in "abcdef".


    index

    >
    > A secondary question is, how do I use perldoc and the other online docs
    > when I can't remember the name of the function I'm looking for? For
    > example I remembered substr(), but that goes the other way, returning
    > the string occurring at a particular character position.


    By reading the "Perl Functions by Category" section of the perlfunc
    documentation and checking the functions in the approproate
    category.

    Or by searching for words probably used to describe it (since you know
    what it does you should be able to make a few guesses). For this
    example I'd be searching for "substring", "search", "position", "index",
    and so on. Either by grepping the documentation on my machine or
    via google.

    >
    > And my third question is, how can I do this as part of a pattern match?
    > For example I want to do something like
    >
    > if ($foo =~ /\d\d/) {
    > # do stuff
    > }
    >
    > where what I really want to know is WHERE within $foo, a pair of digits
    > occurred.


    See the 'pos' function, though you need to change things to use /g to
    use it (which is a significant change).

    See the perlre documentation.

    You can also do something like /^(.*)\d\d/ and check length($1) (assuming no
    \n characters in the string).

    --
    Sam Holden
     
    Sam Holden, Sep 24, 2004
    #2
    1. Advertising

  3. fishfry

    Ala Qumsieh Guest

    Sam Holden wrote:
    > Either by grepping the documentation on my machine or via google.


    I've always wished that perldoc would support something like that. It's
    kind of a -q but for perlfunc text, and would return a list of function
    names whose texts match the query regexp.

    Maybe one day, when I muster enough courage, I'll submit a patch.

    --Ala
     
    Ala Qumsieh, Sep 24, 2004
    #3
  4. fishfry

    Bart Lateur Guest

    fishfry wrote:

    >And my third question is, how can I do this as part of a pattern match?
    >For example I want to do something like
    >
    > if ($foo =~ /\d\d/) {
    > # do stuff
    > }
    >
    >where what I really want to know is WHERE within $foo, a pair of digits
    >occurred.


    Read perlvar, for the arrays @- and @+ . For example, $-[1] holds the
    offset where $1 starts, and $+[1] where it ends. The array index 0 is
    used for the whole match, so you should be interested in $-[0] .

    --
    Bart.
     
    Bart Lateur, Sep 24, 2004
    #4
    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. juli
    Replies:
    3
    Views:
    8,869
    Shiva
    Dec 6, 2004
  2. Ajay

    progrm to find a substring in a string

    Ajay, Mar 22, 2006, in forum: C Programming
    Replies:
    28
    Views:
    845
    Default User
    Mar 23, 2006
  3. Replies:
    1
    Views:
    446
    Terry Reedy
    Jun 22, 2008
  4. Replies:
    3
    Views:
    204
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    211
Loading...

Share This Page