... it has something to do with the regex order ...
The regexes actually being used are all anchored with ^ $ assertions (see perlre, perlretut). That means that certain characters have to appear at the beginning or end of certain strings for a match to occur.
I'm also unsure of just what you expect to get, but consider this code:
Win8 Strawberry 5.8.9.5 (32) Sun 09/05/2021 22:17:31
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings
use Data::Dump qw(dd);
my @words = ( # added a few extra 'words'
'', 'x', 'a', 's', 'aa', 'as', 'es', 'is', 'os', 'sh', 'si', 'so'
);
dd 'WORDS:', \@words;
print "----------------------------\n";
for my $regex (".?a?", "a?.?", "s?.?", ".?s?",) {
print "Matches for ACTUAL regex pattern : ^$regex\$\n\n";
foreach my $word (@words) {
my $sorted = join '', sort split //, $word;
if ($sorted =~ /^$regex$/) {
print "'$sorted' -> '$word' ";
}
}
print "\n----------------------------\n";
}
^Z
(
"WORDS:",
["", "x", "a", "s", "aa", "as", "es", "is", "os", "sh", "si", "so"],
)
----------------------------
Matches for ACTUAL regex pattern : ^.?a?$
'' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' 'aa' -> 'aa'
----------------------------
Matches for ACTUAL regex pattern : ^a?.?$
'' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' 'aa' -> 'aa' 'as' -> 'a
+s'
----------------------------
Matches for ACTUAL regex pattern : ^s?.?$
'' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's'
----------------------------
Matches for ACTUAL regex pattern : ^.?s?$
'' -> '' 'x' -> 'x' 'a' -> 'a' 's' -> 's' 'as' -> 'as' 'es' -> 'e
+s' 'is' -> 'is' 'os' -> 'os' 'hs' -> 'sh' 'is' -> 'si' 'os' -> '
+so'
----------------------------
Note that I've added '' (empty string) 'x' 'a' 's' to the @words array. Note also that all the regexes in question match all these new strings.
The complete regex ^.?a?$ matches any zero- or one-character string. The regex can match some two-character strings. For such a match, an 'a' must be at the absolute end of the string (or before a newline at the end of the string). There's only one two-character string in @words that, after the string is sorted, matches this regex: 'aa'.
(Update: Neither this regex nor any of the others discussed can match a string of more than two characters.)
The regex ^a?.?$ matches any zero- or one-character string and some two-character strings. For a two-character match, an 'a' must be at the absolute beginning of the string. There are two, two-character strings in @words that, after being sorted, match this regex: 'aa' 'as'.
The regex ^s?.?$ doesn't match any two-character strings because no string in @words, after being sorted, begins with an 's'.
The regex ^.?s?$ matches almost every two-character string in @words because almost every such string, after being sorted, ends in 's'.
Give a man a fish: <%-{-{-{-<
|