2
$\begingroup$

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?

$\endgroup$
0

1 Answer 1

3
$\begingroup$

Look at the FullForm of the pattern; you will notice that it becomes a StringExpression:

FullForm[___ ~~ {"1", "4"} ~~ ___]

(* Out: StringExpression[BlankNullSequence[], List["1", "4"], BlankNullSequence[]] *)

The second bullet point in the "Details" section of the documentation of StringExpression lists the types of objects that can appear in StringExpression, among which is

"{$p_1$, $p_2$, ...} or $p_1$ | $p_2$ |... -> a pattern matching at least one of the $p_i$".

For your second question, I interpret Alternatives[{"1", "4"}] to mean "a pattern object that represents any of the patterns {"1", "4"}", which I would then interpret as the pattern {"1", "4"} itself. Then you go back to what we discussed above.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.