When using the same name for part of patterns inside Alternatives
how come it behaves differently for strings than in normal patterns?
{StringMatchQ["ab", (x_ ~~ "c") | ("a" ~~ x_)],
StringMatchQ["ab", ("a" ~~ x_) | (x_ ~~ "c")]}
(* False, True *)
{MatchQ[{1, 2}, {x_, 3} | {1, x_}],
MatchQ[{1, 2}, {1, x_} | {x_, 3}]}
(* True, True *)
StringMatchQ["ab", (x_ ~~ "c") | (x_ ~~ "b")]
(* False *)
Answer
As @OleksandrR pointed out string patterns are translated to PCRE
The tutorial,in the Implementation Details section, mentions PatternConvert
that shows the translation used:
StringPattern`PatternConvert[(x_ ~~ "c") | ("a" ~~ x_)]
(* {"(?ms)(?:(.)c|a(?:\\1))", {{Hold[x], 1}}, {}, Hold[None]} *)
The relevant part here is (?:(.)c|a(?:\1))
when I was expecting something like: (?:(?
Comments
Post a Comment