Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to find a pattern that matches a list of sequences that share a common form. The sequence form is: 1 followed by zero or more nonzero integers, followed by 0. An example list is:


The closest thing I've struck on so far is this:

MatchQ[{1, 2, 0, 1, 2, 0}, {PatternSequence[1, s___ /; FreeQ[{s}, 0], 0] ..}]

which yields True in this case because the sequences are both the same. Is there a way to get the s___ to apply uniquely to each instance in the Repeated? This would solve the problem. Other suggestions are most welcome.

share|improve this question
up vote 4 down vote accepted
list = {1,3,2,0,1,0,1,2,0,1,1,4,0};

MatchQ[list, {PatternSequence[1, ___?(IntegerQ@# && Positive[#] &), 0] ...}]


This is an interesting problem. I'm not sure how to solve it using Condition rather than PatternTest.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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