s/((\n) ([^0-9])+ (-)* (Aa-Zz)*) | ((\n) (\d{3}) (-)* (Aa-Zz)*)/$2$3/gx; # 12 3 4 5 67 8 9 0 #### use strict; use warnings; my $wholeBallOfWax = do {local $/; }; my @records = split /(?<=\n)(?=\d+-)/, $wholeBallOfWax; s/\n+$/\n/s for @records; print join "---\n", @records; __DATA__ 1-12 last non-blank field 2-10 data more data 3-21 stuff more stuff Lots of stuff so much stuff there is no following empty field 4-73 Sneeky record with a blank field in the middle! 5-00 Last record #### 1-12 last non-blank field --- 2-10 data more data --- 3-21 stuff more stuff Lots of stuff so much stuff there is no following empty field --- 4-73 Sneeky record with a blank field in the middle! --- 5-00 Last record