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 allclose all

Basic Examples  (1)

Control ambiguous matches by using Longest:

Scope  (2)

Longest works with string patterns:

Use priorities to affect ambiguous choices:

Applications  (2)

Find the longest sequence of integers:

Consider the following variable length argument function:

Use Longest to express it as one pattern:

Properties & Relations  (4)

For expressions, when a pattern has consecutive sequences, the last one is effectively wrapped in Longest:

For string patterns, Longest provides "greedy" matches of all maximum-length matches:

For ordinary patterns, most patterns are effectively wrapped in Shortest:

String patterns are effectively wrapped in Longest:

Shortest[patt:def] first attempts the pattern using def and only then patt:

When Longest is combined with Optional, the order is reversed:

This is the default behavior when neither wrapper is used:

Possible Issues  (3)

When there are additional constraints, Longest may not match the absolute longest sequence:

For string patterns, Longest will match all maximal substrings, not just the absolute longest substring:

Priorities are not supported in string expressions:

Wolfram Research (2007), Longest, Wolfram Language function, https://reference.wolfram.com/language/ref/Longest.html.

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_2024_longest, author="Wolfram Research", title="{Longest}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Longest.html}", note=[Accessed: 22-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_longest, organization={Wolfram Research}, title={Longest}, year={2007}, url={https://reference.wolfram.com/language/ref/Longest.html}, note=[Accessed: 22-November-2024 ]}