4
$\begingroup$

Why does Shortest not work in a SequencePosition call? The following two commands return the same answer:

SequencePosition[{1, 2, 3, 4, 5}, {a_, ___, b_}]
(*    {{1, 5}, {2, 5}, {3, 5}, {4, 5}}    *)
SequencePosition[{1, 2, 3, 4, 5}, {a_, Shortest[___], b_}]
(*    {{1, 5}, {2, 5}, {3, 5}, {4, 5}}    *)

I would prefer it if the second one returned

(*    {{1, 2}, {2, 3}, {3, 4}, {4, 5}}    *)

Do you know how to achieve this behavior?

Notice that the closely related StringPosition works as expected:

StringPosition["12345", a_ ~~ ___ ~~ b_]
(*    {{1, 5}, {2, 5}, {3, 5}, {4, 5}}    *)
StringPosition["12345", a_ ~~ Shortest[___] ~~ b_]
(*    {{1, 2}, {2, 3}, {3, 4}, {4, 5}}    *)

Related:

$\endgroup$
  • $\begingroup$ Forwarded to Wolfram Support as unexpected behavior. $\endgroup$ – Roman Jun 9 at 11:43
  • $\begingroup$ Confirmed by Wolfram Support as unexpected behavior [CASE:4269719]. $\endgroup$ – Roman Jun 13 at 7:53
  • $\begingroup$ It's no surprise StringPosition doesn't share the problem - it is based on a common (textual) regular expression matcher (was it PCRE?), while the pattern matcher in Mathematica is their own. $\endgroup$ – kirma Jun 13 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.