#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11106665 use warnings; my $string_three = 'foo bar [21a] plus (b23) baz bax'; my @string_three_results = $string_three =~ m{ ( [ab] | \d+ ) ( (??{$1 lt 'a' ? '[ab]' : '\\d+' }) ) }gx; use Data::Dump 'dd'; dd \@string_three_results; #### [21, "a", "b", 23]