且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-20 22:13:47

全局匹配并将匹配数量与3

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.