use strict;
my $str = 'Created a new subnet:
+-------------------+------------------------------------------------+
| Field | Value |
+-------------------+------------------------------------------------+
| allocation_pools | {"start": "10.42.42.2", "end": "10.42.42.254"} |
| cidr | 10.42.42.0/24 | | dns_nameservers | | | enable_dhcp | True |
| gateway_ip | 10.42.42.1 |
| host_routes | |
| id | 3eded702-2909-4515-bf74-7c7c2c7c96e3 |
| ip_version | 4 |
| ipv6_address_mode | |
| ipv6_ra_mode | |
| name | Atelier |
| network_id | 5d15b14c-1d39-48d5-8f48-0dfd68c98d47 |
| tenant_id | d3c17d0e8bd742ed939794a98991886f
| +-------------------+------------------------------------------------+
';
my $regex = qr/(?<=\ id\ \|.)(([a-zA-Z0-9_]{8})-([a-zA-Z0-9_]{4})-([a-zA-Z0-9_]{4})-([a-zA-Z0-9_]{4})-([a-zA-Z0-9_]{12}))/p;
if ( $str =~ /$regex/ ) {
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