Trying to match a pattern which is roughly a-or-b-followed by digits or digits-followed-by-a-or-b.
use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 0; my $string_one = 'foo bar [a21] plus (b23) baz bax'; my @string_one_results = $string_one =~ m { (a|b) (\d+) }gx; print "RESULTS: " , Dumper(\@string_one_results), $/; # RESULTS: $VAR1 = ['a','21','b','23'];
So far so good for the first pattern. Here's the other
my $string_two = 'foo bar [21a] plus (23b) baz bax'; my @string_two_results = $string_two =~ m{ (\d+) (a|b) }gx; print "RESULTS: " , Dumper(\@string_two_results), $/; # RESULTS: $VAR1 = ['21','a','23','b'];
The second regex matches fine for that other format. Here's what I've been struggling over for half an hour or so: what if the two formats are mixed up?
Shouldn't I be able to come up with a regex matching either pattern?
my $string_three = 'foo bar [21a] plus (b23) baz bax'; my @string_three_results = $string_three =~ m{ (\d+) (a|b) | (a|b) (\d ++) }gx; print "RESULTS: " , Dumper(\@string_three_results), $/; # RESULTS: $VAR1 = ['21','a',undef,undef,undef,undef,'b','23'];
Please help me out, Monks. What am I doing wrong and what are the undefs in my @results?
In reply to Complex regex question by Cody Fendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |