How to match string that contain exact 3 time occurrence of special character in perl

前端 未结 4 446
梦谈多话
梦谈多话 2021-01-20 13:40

I have try few method to match a word that contain exact 3 times slash but cannot work. Below are the example

@array = qw( abc/ab1/abc/abc a2/b1/c3/d4/ee w/5         


        
4条回答
  •  一个人的身影
    2021-01-20 14:29

    The pattern you need (with whitespace added) is

    ^ [^/]* / [^/]* / [^/]* / [^/]* \z
    

    or

    ^ [^/]* (?: / [^/]* ){3} \z
    

    Your second attempt was close, but using ^ without \z made it so you checked for string starting with your pattern.


    Solutions:

    say for grep { m{^ [^/]* (?: / [^/]* ){3} \z}x } @array;
    

    or

    say for grep { ( () = m{/}g ) == 3 } @array;
    

    or

    say for grep { tr{/}{} == 3 } @array;
    

提交回复
热议问题