SequenceReplace

SequenceReplace[list,rules]

replaces sequences of list by applying a rule or list of rules.

SequenceReplace[list,rules,n]

does only the first n replacements.

SequenceReplace[rules]

represents an operator form of SequenceReplace that can be applied to an expression.

Details

  • In SequenceReplace[list,patt->lhs], patt can be an expression with head List that includes pattern constructs such as PatternSequence, Repeated (..), Blank (_) and BlankSequence (__).
  • The pattern patt can also be a pattern that matches a list but does not itself have head List.
  • SequenceReplace goes through a list, testing sequences that start at each successive element position. On each sequence, it tries in turn each of the transformation rules that were specified. If any of the rules apply, it replaces the sequence, then continues to go through the list, starting at the element position after the end of the sequence.
  • SequenceReplace[rules][list] is equivalent to SequenceReplace[list,rules].

Examples

open allclose all

Basic Examples  (4)

Replace sequences that match a pattern:

In[1]:=
Click for copyable input
Out[1]=

Replace sequences that match any of the patterns:

In[1]:=
Click for copyable input
Out[1]=

Replace only the first 2 matches of the rules:

In[1]:=
Click for copyable input
Out[1]=

Use the operator form of SequenceReplace:

In[1]:=
Click for copyable input
Out[1]=

Scope  (4)

Applications  (4)

Properties & Relations  (5)

See Also

SequenceCases  SequenceSplit  SequencePosition  SequenceCount  Replace  StringReplace  PatternSequence  Subsequences  SequenceAlignment

Introduced in 2018
(11.3)