use strict;
my $str = 'field {
id : 536870914
name : Set Field (Submit Mode)
datatype : 4
fieldtype : 1
create-mode : 2
option : 2
timestamp : 1489159658
owner : John Smith
last-changed : John Smith
length-units : 0
maxlength : 0
clob-store-opt : 0
menu-style : 1
qbe-match-op : 1
fulltext-optns : 0
permission : 12\\1
}
field {
id : 536870915
name : Schema Name
datatype : 4
fieldtype : 1
create-mode : 2
option : 1
timestamp : 1165057260
owner : John Smith
last-changed : John Smith
length-units : 0
maxlength : 30
clob-store-opt : 0
menu-style : 1
qbe-match-op : 1
fulltext-optns : 0
permission : 12\\1
}
field {
id : 536870916
name : Type
datatype : 4
fieldtype : 1
create-mode : 2
option : 1
timestamp : 1165057260
owner : John Smith
last-changed : John Smith
length-units : 0
maxlength : 30
clob-store-opt : 0
menu-style : 2
qbe-match-op : 1
fulltext-optns : 0
permission : 12\\1
}
field {
id : 536870917
name : Set Field (Query Mode)
datatype : 4
fieldtype : 1
create-mode : 2
option : 2
timestamp : 1489159658
owner : John Smith
last-changed : John Smith
length-units : 0
maxlength : 0
clob-store-opt : 0
menu-style : 1
qbe-match-op : 1
fulltext-optns : 0
permission : 12\\1
}';
my $regex = qr/(datatype\s+: 4\b(?:(?!datatype\s+: \d)[\s\S])*?(maxlength\s+:\s0\b))/p;
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