use strict; use warnings; # Slurp data. # { local $/ = undef; $_ = ; } # Construct regular expression to pull out each data # item; use extended syntax and single-line matching # (i.e. a . matches newline). # my $rxExtract = qr{(?xs) (.+?) # capture one or more characters # with non-greedy matching (?= # zero-width look ahead (?: # alternation group, either \d+\t # digits then a tab | # or \z # end of string ) # close alternation ) # close look-ahead }; # Global match to pull data items out of string. Then # replace all but the last newline in each data item # with tabs. Print items out. # my @items = /$rxExtract/g; s/\n(?=.)/\t/g for @items; print @items; __END__ 1 John Doe, Joe Bloggs title journal Animal Female Protein 2 Mary Clary title magazine Fish Nor Fowl 3 Charley Farley, Piggy Malone title book The Phantom Raspberry Blower #### 1 John Doe, Joe Bloggs title journal Animal Female Protein 2 Mary Clary title magazine Fish Nor Fowl 3 Charley Farley, Piggy Malone title book The Phantom Raspberry Blower