Pattern Sequences

In some cases you may need to specify pattern sequences that are more intricate than things like x__ or x..; for such situations you can use (RefLink[PatternSequence, paclet:ref/PatternSequence])[(Subscript[p, 1]),(Subscript[p, 2]),…].

Pattern sequences.

This defines a function with two or more arguments, grouping the first two:

f[x:PatternSequence[_,_],y___]:=p[{x},{y}]

Evaluate the function for different numbers of arguments:

{f[1],f[1,2],f[1,2,3,4,5]}

This picks out the longest run of the sequence a,b in the list:

{a,b,b,a,b,a,b,a,a,b}/.{___,x:Longest[PatternSequence[a,b]..],___}:>{x}

The empty sequence, (RefLink[PatternSequence, paclet:ref/PatternSequence])[], is sometimes useful to specify an optional argument.

This picks out expressions with exactly one or two arguments:

{g[],g[1],g[1,2],g[1,2,3]}/.x:g[_,_|PatternSequence[]]:>p[x]