use strict;
my $str = 'S3007/0011
20150590824
HARRY MOSES
F
32
DIVISION 4
CIV-\'F\' HIST-\'D\' GEO-\'F\' KISW-\'D\' ENGL-\'D\' PHY-\'F\' CHEM-\'F\' BIO-\'F\' B/MATH-\'F\'
S3007/0012
20151514797
HARRY MORGAN
F
26
DIVISION 3
CIV-\'D\' HIST-\'C\' GEO-\'D\' KISW-\'C\' ENGL-\'C\' PHY-\'F\' CHEM-\'F\' BIO-\'D\' B/MATH-\'F\'
S3007/0012
20151514797
DIVISION MORGAN
F
26
HARRY 3
CIV-\'D\' HIST-\'C\' GEO-\'D\' KISW-\'C\' ENGL-\'C\' PHY-\'F\' CHEM-\'F\' BIO-\'D\' B/MATH-\'F\'
S3007/0013
20151514798
HAPPINESS DEOGRATIAS
F
34
DIVISION 0
CIV-\'F\' HIST-\'F\' GEO-\'F\' KISW-\'D\' ENGL-\'F\' PHY-\'F\' CHEM-\'F\' BIO-\'F\' B/MATH-\'F';
my $regex = qr/^((?:.+\n)*?.*(?:(HARRY)(?:(?!\n\n)(?:.|\n))*?(DIVISION)).*(?:\n.+)*)$/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html