open(FILE, 'C:\Users\li\perl\animals.txt'); $/ = " "; while () { if (m/(?=.*((\d)+?))(?=.*((?<=#)(.*?)(?= fin)))/g) { print "$1\n"; } }