SequenceReplace

SequenceReplace[list,rules]

replaces sequences in list according to the specified 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,pattrhs], 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:

Replace sequences that match any of the patterns:

Replace only the first 2 matches of the rules:

Use the operator form of SequenceReplace:

Scope  (4)

Use one rule of replacement:

Use a list of replacement rules:

Replace the first 2 matches of a rule:

Replace the first 3 matches of all rules:

Applications  (4)

Duplicate elements in a list:

Remove sequences of a list:

Exchange elements two by two:

Keep the first element of each run of duplicates:

Properties & Relations  (6)

Replace a sequence with a list of elements:

Use Sequence instead of List to splice the elements in the original list:

Rules are tried in the given order:

This replacement matching sequences of length 3 can only act twice on a list of length 6:

Replace a sequence by an expression:

Find the position of the sequence:

Find the actual sequence:

Count the number of cases:

For lists of characters, SequenceReplace is similar to StringReplace:

SubsetReplace matches the elements of the sequence in any order:

SequenceReplace matches the sequence only in the order specified:

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

Text

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

CMS

Wolfram Language. 2018. "SequenceReplace." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SequenceReplace.html.

APA

Wolfram Language. (2018). SequenceReplace. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SequenceReplace.html

BibTeX

@misc{reference.wolfram_2024_sequencereplace, author="Wolfram Research", title="{SequenceReplace}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/SequenceReplace.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_sequencereplace, organization={Wolfram Research}, title={SequenceReplace}, year={2018}, url={https://reference.wolfram.com/language/ref/SequenceReplace.html}, note=[Accessed: 21-November-2024 ]}