I am trying to match a list of strings with a pattern, in which there are several possibilities for the characters to be matched. To me, the following looks the most natural way:
StringMatchQ[{"12", "23", "34", "45"}, ___ ~~ Alternatives @@ {"1", "4"} ~~ ___]
what returns as expected:
{True, False, True, True}
However, I've found out the following does work too (and it's faster when the list is very large):
StringMatchQ[{"12", "23", "34", "45"}, ___ ~~ {"1", "4"} ~~ ___]
{True, False, True, True}
Why does this work? Why are we able to specify the different possibilities with a list instead of with Alternatives?.
I have also found out we do not even need to evaluate Alternatives properly, the following does work as well:
StringMatchQ[{"12", "23", "34", "45"}, ___ ~~ Alternatives @ {"1", "4"} ~~ ___]
{True, False, True, True}
even though the head of List
is not replaced:
Alternatives@{"1", "4"}
what yields the (undefined?) symbolic expression:
Alternatives[{"1", "4"}]
Why these last two ways work and where is it documented?