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

前端 未结 4 442
梦谈多话
梦谈多话 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:34

    Match globally and compare the number of matches with 3

    if ( ( () = m{/}g ) == 3 ) { say "Matched 3 times" }
    

    where the =()= operator is a play on context, forcing list context on its right side but returning the number of elements of that list when scalar context is provided on its left side.

    If you are uncomfortable with such a syntax stretch then assign to an array

    if ( ( my @m = m{/}g ) == 3 ) { say "Matched 3 times" }
    

    where the subsequent comparison evaluates it in the scalar context.

    You are trying to match three consecutive / and your string doesn't have that.

提交回复
热议问题