作者newnewest (newnewest)
看板Perl
标题Re: [问题] 字串比对次数?
时间Mon Nov 24 12:18:16 2008
※ 引述《deh3215 ()》之铭言:
: 举例:
: #!/usr/bin/perl
: $str="http://secure.ooo.com.secure.ijm.secure";
: if ($num=$str =~/(secure)*/) {
: print $num;
: }
: 希望可以印出比对secure的次数
: 不论是$num=$str =~/(secure)*/
: 或是$num=$str =~/secure*/
: 或是$num=$str =~/secure/
: $num值都为1..应该是3 囧
要用 match global 吧..
if (@num = $str =~/secure/g)
{
print scalar(@num);
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 67.188.125.61
1F:→ deh3215:另一种写法,感谢题供不同的解 11/24 13:37
2F:→ giacch:其实这才是原问题的解答... (逃~~~ 11/24 14:03
3F:→ deh3215:g兄的方法比较巧妙 这篇的解法比较正规(找不到形容词) 11/24 17:31
4F:推 deh3215:另外print $#num+1也可以印出3 scalar是指阵列中元素个数? 11/24 21:57
5F:→ deh3215:n兄的作法和自己一开始想的一样,不知道perl有全域比对 囧" 11/24 22:04