Longest[p]
is a pattern object that matches the longest sequence consistent with the pattern p.
Longest
Longest[p]
is a pattern object that matches the longest sequence consistent with the pattern p.
Details
- Longest works for both ordinary expression patterns and string patterns.
- If several Longest objects occur in the same expression, those that appear first are given higher priority to match longest sequences.
- Longest[p,pri] is given priority pri to be the longest sequence. Matches for longest sequences are tried first for Longest objects with higher priorities.
- Priorities can be any expression, and are ordered in standard Wolfram Language Sort order. Longest[p] specifies the highest possible priority.
- Longest objects with equal priorities are tried in the order they appear in the expression.
- If no explicit Longest or Shortest is given, ordinary expression patterns are normally effectively assumed to be Shortest[p], while string patterns are assumed to be Longest[p]. »
- Longest has special meaning when combined with Optional. Longest[patt:def] first attempts to match the pattern patt and only then tries the value def. If Optional is used with no explicit Longest or Shortest, it is effectively wrapped in Longest. »
- For ordinary expressions, Longest[p] specifies that not just p itself, but also all parts of p should match the longest sequences.
- Longest[p] corresponds to a "greedy pattern".
- Longest[p] may not correspond to the absolutely longest expression matching p if there are additional constraints elsewhere in the pattern.
Examples
open all close allBasic Examples (1)
Control ambiguous matches by using Longest:
{a, b, c, d, e, f, g} /. {x__, Longest[y__]} -> {{x}, {y}}{a, b, c, d, e, f, g} /. {Longest[x__], y__} -> {{x}, {y}}Scope (2)
Longest works with string patterns:
StringCases["abcdefg", x__ ~~ Longest[__] :> x]Use priorities to affect ambiguous choices:
{a, b, c, d, e, f, g} /. {Longest[x__, 2], Longest[y__, 1]} :> {{x}, {y}}{a, b, c, d, e, f, g} /. {Longest[x__, 1], Longest[y__, 2]} :> {{x}, {y}}Applications (2)
Find the longest sequence of integers:
{x, 3, 5, 7, y, 1, 2, 3, 4, z} /. {a___, Longest[b__Integer], c___} :> {b}Consider the following variable length argument function:
g[b_] := {x, b};g[a_, b_] := {a, b};{g[1], g[1, 2]}Use Longest to express it as one pattern:
f[Longest[a_ : x, 1], Longest[b_, 2]] := {a, b}{f[1], f[1, 2]}Properties & Relations (4)
For expressions, when a pattern has consecutive sequences, the last one is effectively wrapped in Longest:
Replace[{a, b, c, d, e, f}, {x__, y__} :> {y}]Replace[{a, b, c, d, e, f}, {x__, Longest[y__]} :> {y}]For string patterns, Longest provides "greedy" matches of all maximum-length matches:
StringCases["0111011110", Longest["1"..]]For ordinary patterns, most patterns are effectively wrapped in Shortest:
ReplaceList[g[a -> b], g[x___, o___] :> {{x}, {o}}] === ReplaceList[g[a -> b], g[Shortest[x___], Shortest[o___]] :> {{x}, {o}}]String patterns are effectively wrapped in Longest:
StringCases["axxxx", p : ("a" ~~ Repeated["x"]) :> p ] === StringCases["axxxx", p : ("a" ~~ Longest[Repeated["x"]]) :> p ]Shortest[patt:def] first attempts the pattern using def and only then patt:
ReplaceList[{1, 2, 3, 4}, {x___, Shortest[y_ : 0]} :> {y}]When Longest is combined with Optional, the order is reversed:
ReplaceList[{1, 2, 3, 4}, {x___, Longest[y_ : 0]} :> {y}]This is the default behavior when neither wrapper is used:
ReplaceList[{1, 2, 3, 4}, {x___, y_ : 0} :> {y}]Possible Issues (3)
When there are additional constraints, Longest may not match the absolute longest sequence:
Cases[{{16, 1, 2, 1, 3, 2, 1, 3, 5, 2, 17}}, {__, a : Longest[PatternSequence[1, __, 2]], ___} :> {a}]Cases[{{16, 1, 2, 1, 3, 2, 1, 3, 5, 2, 17}}, {Longest[__], a : Longest[PatternSequence[1, __, 2]], ___} :> {a}]For string patterns, Longest will match all maximal substrings, not just the absolute longest substring:
StringReplace["011011110", Longest["1"..] :> "x"]Priorities are not supported in string expressions:
StringReplace["abcdefg", Longest[x__, 1] ~~ y__ -> x ~~ "X" ~~ y]Tech Notes
Related Guides
History
Text
Wolfram Research (2007), Longest, Wolfram Language function, https://reference.wolfram.com/language/ref/Longest.html.
CMS
Wolfram Language. 2007. "Longest." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Longest.html.
APA
Wolfram Language. (2007). Longest. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Longest.html
BibTeX
@misc{reference.wolfram_2026_longest, author="Wolfram Research", title="{Longest}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Longest.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_longest, organization={Wolfram Research}, title={Longest}, year={2007}, url={https://reference.wolfram.com/language/ref/Longest.html}, note=[Accessed: 12-June-2026]}