This is documentation for Mathematica 7, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Longest

Longest[p]
is a pattern object that matches the longest sequence consistent with the pattern p.
  • 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 Mathematica 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].
  • For ordinary expressions, Longest[p] specifies that not just p itself, but also all parts of p should match the longest sequences.
Require that y__ matches the longest sequence it can:
The default is to have earlier patterns match shortest sequences:
Require that y__ matches the longest sequence it can:
In[1]:=
Click for copyable input
Out[1]=
The default is to have earlier patterns match shortest sequences:
In[2]:=
Click for copyable input
Out[2]=
Use priorities to affect ambiguous choices:
Find the longest sequence of integers:
Consider the following variable length argument function:
Use Longest to express it as one pattern:
For strings, the default is to have earlier patterns match longest character sequences:
This is equivalent:
Priorities are not supported in string expressions:
New in 6