@f='chapter 01,02,03,04'; foreach(@f) { if($_=~m/,(\d)+|(\d)+,(\d)+|(\d)+,(\d)+,(\d)+/i) { print "$& \n"; } }