Рассмотрим следующий пример, который зависит от того, какие блоки описания курса полностью содержатся в том, что Perl считает абзацами:
#! /usr/bin/perl
$/ = "";
my $record_start = qr/
^ # starting with a newline
\s* # allow optional leading whitespace
([A-Z]+\d+) # capture course tag, e.g., ARTA215
\s+ # separating whitespace
(.+?) # course title on rest of line
\s*\n # consume trailing whitespace
/mx;
while (<>) {
my($course,$title);
if (s/\A$record_start//) { # fix Stack Overflow highlighting /
($course,$title) = ($1,$2);
}
elsif (s/(?s:^.+?)(?=$record_start)//) { # ditto /
redo;
}
else {
next;
}
my $desc;
die unless s/^(.+?)(?=$record_start|\s*$)//s;
(my $desc = $1) =~ s/\s*\n\s*/ /g;
for ($course, $title, $desc) {
s/^\s+//; s/\s+$//; s/\s+/ /g;
}
print join("," => map qq{"$_"} => $course, $title, $desc), "\n";
redo if $_;
}
Когда подается ваш ввод данных, он выводит
"ARTA215","ADVANCED LIFE DRAWING (3 Cr) (2:2) + Studio 1 hr.","This advanced study in drawing with the life .... Prerequisite: ARTA150 Lab Fee Required"
"ARTA220","CERAMICS II (3 Cr) (2:2) + Studio 1 hr.","This course affords the student the opportunity to ex... Lab Fee Required"
"ARTA250","SPECIAL TOPICS IN ART","This course focuses on selected topic...."
"ARTA260","PORTFOLIO DEVELOPMENT (3 Cr) (3:0)","The purpose of this course is to pre...."
"BIOS010","INTRODUCTION TO BIOLOGICAL CONCEPTS (3IC) (2:2)","This course is a preparatory course designed to familiarize the begi...."
"BIOS101","GENERAL BIOLOGY (4 Cr) (3:3)","This course introduces the student to the principles of mo... Lab Fee Required"
"BIOS102","INTRODUCTION TO HUMAN BIOLOGY (4 Cr) (3:3)","This course is an introd.... Lab Fee Required"