use 5.010; use strict; use warnings; while () { chomp; say "$_ ", /(<+)[^<>]*(>+)(*COMMIT)(?(?{length($1) == length($2)})|(*FAIL))/ ? "matches" : "does not match"; } __DATA__ <1> <<2>> <<3> <<<<4>> <<5>>>> #### <1> matches <<2>> matches <<3> does not match <<<<4>> does not match <<5>>>> does not match