I'm working through the exercises in Stephen Wolfram's book "An Elementary Introduction to the Wolfram Language." In Chapter 32 on "Patterns", Exercise 32.1 is "Find lists beginning with 1 and ending with 9 in IntegerDigits[Range[1000]]." and the answer given is Cases[IntegerDigits[Range[1000]], {1, __, 9}]. However, this produces only the lists for 3-digit numbers like 109, but it doesn't produce 19, {{1},{19}}, the only 2-digit number in the range. To get a complete list (including 19), I use Intersection[Cases[IntegerDigits[Range[1000]], {1, __}], Cases[IntegerDigits[Range[1000]], {__, 9}]]. I also triedCases[IntegerDigits[Range[1000]], {1, x_: Nothing, 9}],which produces the correct list in this case (including 19), but fails for cases where the list contains numbers with 4 or more digit numbers. My question: instead of '__', which matches any number of items but fails to find lists where there is no item in that slot, is there a pattern-matching symbol that will find zero or more items at a pattern slot?

  • 5
    $\begingroup$ Yes, BlankNullSequence[], ___. Its documentation page is here. $\endgroup$ – Oleksandr R. Jan 3 '16 at 0:05

Browse other questions tagged or ask your own question.