http://www.perlmonks.org?node_id=747897


in reply to Multiple Line Data from text file to SQL

# Slurp the file my $Text=do { local $/; <DATA> }; # Assuming that a question begins with a number followed by a period: # Then a question is a question number followed by lines which are not + question numbers. #while ($Text =~ m{(\d+\.\n([^\n\r\f]*[a-zA-Z]+[^\n\r\f]*\n)+)}s) { while ($Text =~ m{((\d+)\.\n(([^\n\r\f]*[a-zA-Z]+[^\n\r\f]*\n)+)(A\)([ +^\n\r\f]*[a-zA-Z]+[^\n\r\f]*\n)+?)(B\)([^\n\r\f]*[a-zA-Z]+[^\n\r\f]*\ +n)+?)(C\)([^\n\r\f]*[a-zA-Z]+[^\n\r\f]*\n)+?)(D\)([^\n\r\f]*[a-zA-Z]+ +[^\n\r\f]*\n)+))}gs) { ### question number :$2 ### question :$3 ### A) :$5 ### B) :$7 ### C) :$9 ### D) :$11 #using g so #$Text=$'; };
works but assumes that a question number of the form \d+\. begins each question, the answers must be A, B, C and D and of the form x) ..., and none of the answers are split so that the trailing portion resembles a question number.