如何在perl中匹配包含特殊字符的精确3次出现的字符串

Tim*_*Tim 3 regex perl metacharacters

我尝试了一些方法来匹配一个包含3次斜线但却无法工作的单词.以下是示例

@array = qw( abc/ab1/abc/abc a2/b1/c3/d4/ee w/5/a  s/t )
foreach my $string (@array){
    if ( $string =~ /^\/{3}/ ){
          print " yes, word with 3 / found !\n";
          print "$string\n";
    }
    else {
          print " no word contain 3 / found\n";
    }
Run Code Online (Sandbox Code Playgroud)

很少有macthing我尝试但没有一个工作

$string =~ /^\/{3}/;
$string =~ /^(\w+\/\w+\/\w+\/\w+)/;
$string =~ /^(.*\/.*\/.*\/.*)/;
Run Code Online (Sandbox Code Playgroud)

任何其他方式我可以匹配这种类型的字符串并打印字符串?

zdi*_*dim 9

全局匹配并比较匹配数 3

if ( ( () = m{/}g ) == 3 ) { say "Matched 3 times" }
Run Code Online (Sandbox Code Playgroud)

其中=()=运算符是上下文的播放,强制列表上下文在其右侧,但在其左侧提供标量上下文时返回该列表的元素数.

如果您对这样的语法拉伸分配给数组感到不舒服

if ( ( my @m = m{/}g ) == 3 ) { say "Matched 3 times" }
Run Code Online (Sandbox Code Playgroud)

后续比较在标量上下文中对其进行评估.

你试图连续 三个匹配/,你的字符串没有.