use strict; use warnings; while () { print if (/d1/ .. /d4/); } __DATA__ a b c d d1 d2 d3 d4 a b d1 d2 d4 a f g #### d1 d2 d3 d4 d1 d2 d4