Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Primary Navigation

    • Cloud & Deployment
    • Core Language & Structure
    • Data Manipulation & Analysis
    • Engineering Data & Computation
    • External Interfaces & Connections
    • Financial Data & Computation
    • Geographic Data & Computation
    • Geometry
    • Graphs & Networks
    • Higher Mathematical Computation
    • Images
    • Knowledge Representation & Natural Language
    • Machine Learning
    • Notebook Documents & Presentation
    • Scientific and Medical Data & Computation
    • Social, Cultural & Linguistic Data
    • Strings & Text
    • Symbolic & Numeric Computation
    • System Operation & Setup
    • Time-Related Computation
    • User Interface Construction
    • Visualization & Graphics
    • Random Paclet
    • Alphabetical List
  • Using Paclets
    • Get Started
    • Download Definition Notebook
  • Learn More about Wolfram Language

PatternMatcher

Guides

  • A Virtual Machine for the Pattern Matcher

Symbols

  • CompilePatternToBytecode
  • CreatePatternMatcherVirtualMachine
  • PatternBytecodeDisassemble
  • PatternBytecodeInformation
  • PatternBytecode
  • PatternBytecodeQ
  • PatternMatcherEnableTrace
  • PatternMatcherExecute
  • PatternMatcherMatch
  • PatternMatcherMatchQ
  • PatternMatcherReplace
  • PatternMatcherStep
  • PatternMatcherTraceEnabled
  • PatternMatcherTraceEnabledQ
  • PatternMatcherVirtualMachine
  • PatternToMatchFunction
DanielS`PatternMatcher`
PatternMatcherReplace
​
PatternMatcherReplace
[expr,lhsrhs]
replaces parts of
expr
that match the pattern
lhs
with
rhs
.
​
​
PatternMatcherReplace
[expr,lhsrhs]
uses RuleDelayed, evaluating
rhs
only after binding pattern variables.
​
​
PatternMatcherReplace
[rule]
is an operator form that can be applied to expressions.
​
Details and Options
Examples  
(4)
Basic Examples  
(2)
Replace a literal value:
In[1]:=
PatternMatcherReplace
[5,5"five"]
Out[1]=
five
Replace using a blank pattern:
In[2]:=
PatternMatcherReplace
[42,_Integer"number"]
Out[2]=
number
Replace using a named pattern variable:
In[3]:=
PatternMatcherReplace
["hello",x_{x,x}]
Out[3]=
{hello,hello}
Replace using pattern variables:
In[4]:=
PatternMatcherReplace
[f[1,2],f[x_,y_]{x,y}]
Out[4]=
{1,2}
No replacement when pattern doesn't match:
In[5]:=
PatternMatcherReplace
[f[1,2,3],f[x_,y_]{x,y}]
Out[5]=
f[1,2,3]
Replace using a pattern with alternatives:
In[6]:=
PatternMatcherReplace
[f[5],f[x_Integer|x_Real]{x}]
Out[6]=
{5}
Replace using a pattern with condition:
In[7]:=
PatternMatcherReplace
[5,x_/;x>0f[x]]
Out[7]=
f[5]
​
PatternMatcherReplace
has an operator form:
In[1]:=
PatternMatcherReplace
[f[x_Integer,y_String]{x,y}][f[42,"hello"]]
Out[1]=
{42,hello}
Scope  
(2)

SeeAlso
PatternMatcherVirtualMachine
 
▪
PatternBytecode
 
▪
PatternMatcherExecute
 
▪
PatternMatcherMatchQ
 
▪
CreatePatternMatcherVirtualMachine
 
▪
CompilePatternToBytecode
 
▪
PatternMatcherEnableTrace
 
▪
PatternMatcherTraceEnabledQ
RelatedGuides
▪
A Virtual Machine for the Pattern Matcher
""

© 2026 Wolfram. All rights reserved.

  • Legal & Privacy Policy
  • Contact Us
  • WolframAlpha.com
  • WolframCloud.com