use strict;
my $str = 'object-group service DM_INLINE_SERVICE_8
service-object tcp destination eq ldap
service-object udp destination eq syslog
service-object object kerberos5-tcp
service-object object kerberos5-udp
service-object object ldap-udp
service-object udp destination eq domain
service-object object ldap-gcs
service-object object TCP_3268
service-object object TCP_3269
service-object object TCP_445
service-object tcp-udp destination eq domain
service-object tcp destination eq ldaps
service-object udp destination eq ntp
service-object object TCP_464
object-group network DM_INLINE_NETWORK_13
network-object object IN_V030_197_memcache_01
network-object object IN_V030_198_memcache_02
object-group service DM_INLINE_SERVICE_9
service-object tcp destination eq ldap
service-object udp destination eq syslog
service-object object kerberos5-tcp
service-object object kerberos5-udp
service-object object ldap-udp
service-object udp destination eq domain
service-object object ldap-gcs
service-object object TCP_3268
service-object object TCP_3269
service-object object TCP_445
service-object tcp-udp destination eq domain
service-object tcp destination eq ldaps
service-object udp destination eq ntp
service-object object TCP_464
';
my $regex = qr/(object-group service (?P<name>.+)(?:\n |.)|service-object (?P<protocol>.+) destination eq (?P<port_no1>\d{0,5} |\w{0,10}.+)\n|service-object (?P<protocol2>object) (?P<port_no2>\d{0,5} |\w{0,10}.+)\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