Function Repository Resource:

# ReplaceAllOutside

Similar to ReplaceAll, but parts of the expression can be ignored by providing a pattern

Contributed by: Richard Hennigan (Wolfram Research)
 ResourceFunction["ReplaceAllOutside"][expr,rules,except] applies a rule or list of rules in an attempt to transform the entire expression expr while ignoring subexpressions contained in except. ResourceFunction["ReplaceAllOutside"][rules,except] represents an operator form of ResourceFunction["ReplaceAllOutside"] that can be applied to an expression. ResourceFunction["ReplaceAllOutside"][except] represents an operator form of ResourceFunction["ReplaceAllOutside"] that can be applied to arguments.

## Details and Options

In ResourceFunction["ReplaceAllOutside"][expr,rules,except], no replacements will be made in subexpressions of expr that match except.
The rules must be of the form lhsrhs or lhsrhs.
A list of rules can be given. The rules are tried in order. The result of the first one that applies is returned. If none of the rules apply, the original expr is returned.
If the rules are given in nested lists, ReplaceAllOutside is effectively mapped onto the inner lists. Thus ResourceFunction["ReplaceAllOutside"][expr,{{r11,r12},{r21,},},except] is equivalent to {ResourceFunction["ReplaceAllOutside"][expr,{r11,r12},except],ResourceFunction["ReplaceAllOutside"][expr,{r21,},except],}.
Delayed rules defined with can contain /; conditions.

## Examples

### Basic Examples

Replace every instance of an expression, but ignore cases contained in a particular head:

 In[1]:=
 Out[1]=
 In[2]:=
 Out[2]=

Exact matches are also ignored:

 In[3]:=
 Out[3]=

Provide a list of replacement rules:

 In[4]:=
 Out[4]=

Provide nested lists of replacement rules:

 In[5]:=
 Out[5]=

### Scope

Use a Dispatch for replacement rules:

 In[6]:=
 Out[6]=
 In[7]:=
 Out[7]=

Use an Association for replacement rules:

 In[8]:=
 Out[8]=
 In[9]:=
 Out[9]=

### Applications

Convert integers to reals, but ignore those contained in subscripts:

 In[10]:=
 Out[10]=

Replace all non-held integers:

 In[11]:=
 Out[11]=

Get all the strings in an expression, but ignore those that are internal to objects such as Entity and Quantity:

 In[12]:=
 Out[12]=
 In[13]:=
 Out[13]=

## Requirements

Wolfram Language 11.3 (March 2018) or above