开发者

What's the opposite of substr?

I have a text file like so:

dave ran very quickly
dan very slowly ran

I am doing a regex to look for the word "ran" but I also ne开发者_JS百科ed to know where it starts (in the first case it is character 6, in the second case it's 17).

I have (though it isn't much):

for(@lines){

if(/ran/){
 # find where ran is so we can continue parsing

}

} 


It's easy:

my $ran_pos = $-[0];

See the perlvar man page for a detailed description of the @- array.


I believe the index function is what you're looking for.

Here are a couple links:

  • http://perlmeme.org/howtos/perlfunc/index_function.html
  • http://www.misc-perl-info.com/perl-index.html


index STR,SUBSTR will return the position of a substring within a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜