I'm writing a simple regular expression that needs receive a pair of coordinates and/or a map name.
For example:
move 10 15 # should returns [[10, 15]]
move 10 15 map # should returns [[10, 15, 'map']]
move map # should returns [['map']]
move 10 15 mapA mapB # should returns [[10, 15, 'mapA'], ['mapB']]
move 10 15 mapA mapB 33 44 # should returns [[10, 15, 'mapA'], ['mapB'], [33, 44]]
move 10 15 mapA 33 44 mapB # should returns [[10, 15, 'mapA'], [33, 44, 'mapB']]
Then, I wrote this regular expression:
/
(?(DEFINE)
(?<coord> (?<x>\d+)\s+(?<y>\d+) )
(?<map> (?<mapname>[a-zA-Z]+) )
(?<commands> \s* (?: (?&coord) | (?&map) ) \s* (?&commands)? )
)
move\s+(?&commands)
/six
But how I can get the value for groups x
, y
and map
using Perl?
I tried with some ways:
use strict;
use warnings;
my $command = 'move 10 15';
$command =~ /
(?(DEFINE)
(?<coord> (?<x>\d+)\s+(?<y>\d+) )
(?<map> (?<mapname>[a-zA-Z]+) )
(?<commands> \s* (?: (?&coord) | (?&map) ) \s* (?&commands)? )
)
move\s+(?&commands)
/six;
while (my ($k,$v) = each %+) { print "$k $v\n" }
print "$+{x}";