!/usr/bin/perl use strict; use warnings; my $title; while ( ) { chomp; if ( /\(\w+\-\d+\)$/ ) { $title = $_; } else { s/(?:^|\s)(\d\-\d\w{2}(?:\.\w+)?|[A-Z]\d{2}(?:\.\d+)?)/\n$1/g; foreach my $line ( split ( "\n" ) ) { next if $line !~ /^(\d\-\d\w{2}(?:\.\w+)?|[A-Z]\d{2}(?:\.\d+)?)/; print "$title;$line\n"; } } } __DATA__ Titel Text (A12-3) 3-123.7 Just another text 3-123.8 Some more text A12.34 Another item B56.78 Yet another item Another Titel Text (B23-9) 1-22a.b Just another text 2-3cd.e Some more text W12.34 Another item Z56.78 Yet another item Some trash Some trash #### Titel Text (A12-3);3-123.7 Just another text Titel Text (A12-3);3-123.8 Some more text Titel Text (A12-3);A12.34 Another item Titel Text (A12-3);B56.78 Yet another item Another Titel Text (B23-9);1-22a.b Just another text Another Titel Text (B23-9);2-3cd.e Some more text Another Titel Text (B23-9);W12.34 Another item Another Titel Text (B23-9);Z56.78 Yet another item