perl:怎么在一段文字中找到某个字符串的位置(多个位置)保留并输出?
比如说$some="abcbccabc",中找出bc所在的位置并把这些位置保留输出,这个我用index函数只能去第一个找到的位置值,后面的位置就不在找了,怎么能把其他的位...
比如说$some="abcbccabc",中找出bc所在的位置并把这些位置保留输出,这个我用index函数只能去第一个找到的位置值,后面的位置就不在找了,怎么能把其他的位置值也保留并输出啊?谢谢大神们了!!!!!
展开
展开全部
#!/usr/bin/perl
use 5.014;
my $string = "perl berl derl perlperl cerl";
my $key = "perl";
my ( $pos, $now ) = ( 0, -1 );
until ( $pos == -1 ) {
$pos = index( $string, $key, $now + 1 );
$now = $pos;
say $pos unless $pos < 0;
}
use 5.014;
my $string = "perl berl derl perlperl cerl";
my $key = "perl";
my ( $pos, $now ) = ( 0, -1 );
until ( $pos == -1 ) {
$pos = index( $string, $key, $now + 1 );
$now = $pos;
say $pos unless $pos < 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Result: 5
Example 3b. How to find every occurrence
To find (and do something with) every
occurrence of a character in a string, you could
use
index()
in a loop, incrementing the offset each time:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $offset = 0;
my $result = index($string, $char, $offset);
while ($result != -1) {
print "Found $char at $result\n";
$offset = $result + 1;
$result = index($string, $char, $offset);
}
When we run this program, we get the
Example 3b. How to find every occurrence
To find (and do something with) every
occurrence of a character in a string, you could
use
index()
in a loop, incrementing the offset each time:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $offset = 0;
my $result = index($string, $char, $offset);
while ($result != -1) {
print "Found $char at $result\n";
$offset = $result + 1;
$result = index($string, $char, $offset);
}
When we run this program, we get the
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ctrl+f
可以直接搜索这个字符串
可以直接搜索这个字符串
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询