$line[0]="12/123 11-abc 456 a 1/2 zyx this is the last"; $line[1]="14/456 11-abc 456 a 1/2 zyx this is it"; for (@line) { if(m|(\d+/123 11-[a-z]+ \d+)|g) { $start=$1; /\G(.*?) zyx (.*)$/; print "Start of record:$start\nBefore zyx:$1\nand After zyx:$2\n"; } } __OUTPUT__ Start of record:12/123 11-abc 456 Before zyx: a 1/2 and After zyx:this is the last