At the moment, querying the results of a regular expression pattern match using the $0, $1, $2
variables leaves a lot to be desired. As another way of doing things, Rindolf will provide a $^RR
variable which would be a Perl 5 data-structure that is nested to a necessary depth to contain the results of the pattern and sub-pattern matches.
Here's an example:
#!/usr/bin/perl use string; my $string = "[(5,6), (7,3), (8,9)], [(5,7), (2,10)], [(100,92),(14,8)]"; # Define a pattern for (x,y) my $pt_pattern = "\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*"; # Define a pattern for an array of points [pt1,pt2,pt3,pt4] my $array_pattern = "\[(?:${pt_pattern}\s*,)*${pt_pattern}\]"; # Define a pattern for an array of arrays of points arr1,arr2,arr3 my $whole_pattern = "(?:${array_pattern}\s*,)*${array_pattern}" $string =~ /$whole_pattern/; my $results = $^RR; print "a[0][2].x = ", $results->[0]->[2]->[0], "\n"; # prints 8 print "a[1][1].y = ", $results->[1]->[1]->[1], "\n"; # prints 10