use warnings; use strict; use feature 'say'; while () { chomp; if (/1([)|\.])(.*)(2\1)/) { say $2; } else { say "BAD LINE:", $_; } } __DATA__ 1) some text 2) 1. some text 2. 1) some text 2. 1. 2.10.10.20 some text 2. 1) 2.10.10.20 2)