#! /usr/bin/perl use strict; use warnings; my $file = do {local $/, }; my @cvs = $file =~ /((?:(?:[^\n@]+|@[^@]*@)\n?)+)/gs; print "matched [[$_]]\n" for @cvs; __DATA__ line 1 line 2 line 3 longer @line@ with @some stuff@ line with nothing on it line 4