Find location of substring in string?

F

fishfry

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.
 
S

Sam Holden

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).
 
A

Ala Qumsieh

Sam said:
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
 
B

Bart Lateur

fishfry said:
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] .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top