Shortest
Shortest[p]
是与和模式 p 一致的最短序列匹配的模式对象.
更多信息
- Shortest 可用于普通表达式模式和字符模式.
- 如果同一表达式中出现多个 Shortest 对象,则最先出现的对象将被赋予更高的优先级.
- Shortest[p,pri] 被指定为优先级别为 pri 的最短序列. 首先尝试对具有较高优先级的 Shortest 对象进行最短序列的匹配.
- 优先级可以是任意表达式,并且按标准的 Wolfram 语言 Sort 排序. Shortest[p] 指定可能的最高优先级.
- 对于具有同等优先级别的 Shortest 对象,按照它们在表达式中出现的顺序进行尝试.
- 如果没有明确给定 Shortest 或 Longest,普通表达式模式通常被假定为 Shortest[p],而字符串模式被假定为 Longest[p]. »
- 与 Optional 一起使用时,Shortest 有特殊的意义. Shortest[patt:def] 先尝试使用值 def 进行匹配,然后才尝试模式 patt. 如果使用了 Optional,且没有给出明确的 Longest 或 Shortest,则实际上相当于封装在 Longest 中. »
- 对于普通表达式,Shortest[p] 指定不只 p 的本身,而且 p 的所有部分均应匹配最短序列.
- Shortest[p] 对应于“非贪婪模式”.
- 如果模式中有其他约束条件,Shortest[p] 可能不对应于与 p 匹配的绝对最短的表达式.
范例
打开所有单元 关闭所有单元基本范例 (1)
用 Shortest 控制模糊匹配:
{a, b, c, d, e, f, g} /. {x__, Shortest[y__]} :> {{x}, {y}}{a, b, c, d, e, f, g} /. {Shortest[x__], y__} :> {{x}, {y}}范围 (2)
Shortest 适用于字符串模式:
StringCases["abcdefg", Shortest[x__] ~~ __ -> x]StringCases["abcdefg", x__ ~~ __ -> x]{a, b, c, d, e, f, g} /. {Shortest[x__, 2], Shortest[y__, 1]} :> {{x}, {y}}{a, b, c, d, e, f, g} /. {Shortest[x__, 1], Shortest[y__, 2]} :> {{x}, {y}}应用 (1)
g[a_] := {a, x};g[a_, b_] := {a, b};{g[1], g[1, 2]}用 Shortest 将其表示为一个模式:
f[Shortest[a_, 1], Shortest[b_ : x, 2]] := {a, b}{f[1], f[1, 2]}属性和关系 (4)
对于表达式,当模式有连续的序列时,第一个相当于被封装到 Shortest 中:
Replace[{a, b, c, d, e, f}, {x__, y__} :> {x}]Replace[{a, b, c, d, e, f}, {Shortest[x__], y__} :> {x}]对于字符串模式,Shortest 提供所有长度最小的匹配项的 "non-greedy" 或 "lazy" 匹配项:
StringCases["0111011110", Shortest["1"..]]对于普通模式,大部分模式相当于被封装在 Shortest 中:
ReplaceList[g[a -> b], g[x___, o___] :> {{x}, {o}}] === ReplaceList[g[a -> b], g[Shortest[x___], Shortest[o___]] :> {{x}, {o}}]字符串模式则相当于被封装在 Longest 中:
StringCases["axxxx", p : ("a" ~~ Repeated["x"]) :> p ] === StringCases["axxxx", p : ("a" ~~ Longest[Repeated["x"]]) :> p ]默认情况下,patt:def 先尝试用 patt 进行匹配,然后使用 def:
ReplaceList[{1, 2, 3, 4}, {x___, y_ : 0} :> {y}]当 Shortest 与 Optional 一起使用时,优先顺序反转:
ReplaceList[{1, 2, 3, 4}, {x___, Shortest[y_ : 0]} :> {y}]可能存在的问题 (3)
如果还有其他限制条件,Shortest 可能不会匹配绝对最短的序列:
Cases[{{16, 1, 2, 1, 3, 2, 1, 3, 5, 2, 17}}, {__, a : Shortest[PatternSequence[1, __, 2]], ___} :> {a}]Cases[{{16, 1, 2, 1, 3, 2, 1, 3, 5, 2, 17}}, {Longest[__], a : Shortest[PatternSequence[1, __, 2]], __} :> {a}]对于字符串模式,Shortest 将匹配所有最小子串,而不仅仅是绝对最短匹配:
StringReplace["011110A010", Shortest["0" ~~ __ ~~ "0"] :> "x"]StringReplace["abcdefg", Shortest[x__, 1] ~~ __ -> x]文本
Wolfram Research (2007),Shortest,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Shortest.html.
CMS
Wolfram 语言. 2007. "Shortest." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/Shortest.html.
APA
Wolfram 语言. (2007). Shortest. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Shortest.html 年
BibTeX
@misc{reference.wolfram_2026_shortest, author="Wolfram Research", title="{Shortest}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/Shortest.html}", note=[Accessed: 18-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_shortest, organization={Wolfram Research}, title={Shortest}, year={2007}, url={https://reference.wolfram.com/language/ref/Shortest.html}, note=[Accessed: 18-June-2026]}