SequenceCases[list,patt]
gives a list of the sublists in list that match the sequence pattern patt.
SequenceCases[list,pattrhs]
gives a list of the values of rhs corresponding to sublists that match patt.
SequenceCases[list,patt,n]
includes only the first n matches.
SequenceCases
SequenceCases[list,patt]
gives a list of the sublists in list that match the sequence pattern patt.
SequenceCases[list,pattrhs]
gives a list of the values of rhs corresponding to sublists that match patt.
SequenceCases[list,patt,n]
includes only the first n matches.
Details and Options
- In SequenceCases[list,patt], patt can be an expression with head List that includes pattern constructs such as PatternSequence, Repeated (..), Blank (_), and BlankSequence (__).
- The pattern patt can also be a pattern that matches a list but does not itself have head List.
- With the default option setting OverlapsFalse, SequenceCases includes only sublists that do not overlap. With the setting OverlapsTrue, it includes sublists that overlap.
- With Overlaps->All, multiple sublists that match the same pattern are all included. With Overlaps->True, only the first such matching sublist at a given position is included.
Examples
open all close allBasic Examples (2)
Find sublists that match a pattern:
SequenceCases[{a, b, x, x, a, c}, {a, _}]Perform a replacement on each match:
SequenceCases[{a, b, x, x, a, c}, {a, x_} :> f[x]]Count the length of all contiguous sequences of b:
SequenceCases[{a, b, b, a, b, b, b}, {p : Repeated[b]} :> Length[{p}]]Scope (3)
Find sequences of elements that match a pattern:
SequenceCases[Range[1000], {_ ? PrimeQ, _, _ ? PrimeQ}]Find all overlapping sequences in a list:
SequenceCases[Range[10], list_ /; Length[list] == 6, Overlaps -> True]Evaluate a function on the cases found:
SequenceCases[Range[1000], {a_ ? PrimeQ, b_, c_ ? PrimeQ} :> Row[Superscript@@@FactorInteger[b]]]Options (2)
Overlaps (2)
SequenceCases[{a, b, a, b, a, b, a, b}, {a, b, a, b}, Overlaps -> True]SequenceCases[{a, b, a, b, a, b, a, b}, {a, b, a, b}, Overlaps -> False]Allow overlaps starting at different positions:
SequenceCases[{a, b, c, d}, {__}, Overlaps -> True]SequenceCases[{a, b, c, d}, {__}, Overlaps -> False]SequenceCases[{a, b, c, d}, {__}, Overlaps -> All]Properties & Relations (3)
Find the position of a sequence:
SequencePosition[{a, b, 1, 2, c, d, 3, 4}, {__Symbol}, Overlaps -> False]SequenceCases[{a, b, 1, 2, c, d, 3, 4}, {__Symbol}, Overlaps -> False]SequenceCount[{a, b, 1, 2, c, d, 3, 4}, {__Symbol}, Overlaps -> False]Replace the sequence by an expression:
SequenceReplace[{a, b, 1, 2, c, d, 3, 4}, {__Symbol} -> "X"]For lists of characters, SequenceCases is similar to StringCases:
text = "On 31/12/2003 we left, and on 5/3/2004 we came back";SequenceCases[Characters[text], {_ ? LetterQ..}]StringCases[text, LetterCharacter..]SubsetCases matches the elements of the sequence in any order:
SubsetCases[{1, a, 2, 3, a, b, c}, {a, _Integer}]SequenceCases matches the sequence only in the specified order:
SequenceCases[{1, a, 2, 3, a, b, c}, {a, _Integer}]History
Text
Wolfram Research (2015), SequenceCases, Wolfram Language function, https://reference.wolfram.com/language/ref/SequenceCases.html.
CMS
Wolfram Language. 2015. "SequenceCases." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SequenceCases.html.
APA
Wolfram Language. (2015). SequenceCases. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SequenceCases.html
BibTeX
@misc{reference.wolfram_2026_sequencecases, author="Wolfram Research", title="{SequenceCases}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/SequenceCases.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_sequencecases, organization={Wolfram Research}, title={SequenceCases}, year={2015}, url={https://reference.wolfram.com/language/ref/SequenceCases.html}, note=[Accessed: 13-June-2026]}