Wolfram Research

Function Repository Resource:

ReplaceAllOutside

Source Notebook

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]:=
ResourceFunction["ReplaceAllOutside"][{x, f[x], x}, x -> y, _f]
Out[1]=
In[2]:=
ResourceFunction["ReplaceAllOutside"][{g[f[1]], f[2], f[g[3]]}, f[x_] :> x + 1, _g]
Out[2]=

Exact matches are also ignored:

In[3]:=
ResourceFunction["ReplaceAllOutside"][{1, 2, 3, 4, 5}, _Integer :> x,
  3]
Out[3]=

Provide a list of replacement rules:

In[4]:=
ResourceFunction[
 "ReplaceAllOutside"][{x, f[x, a], a}, {x -> y, a -> b}, _f]
Out[4]=

Provide nested lists of replacement rules:

In[5]:=
ResourceFunction[
 "ReplaceAllOutside"][{x, f[x, a], a}, {{x -> y, a -> b}, {x -> 1, a -> 2}}, _f]
Out[5]=

Scope

Use a Dispatch for replacement rules:

In[6]:=
dispatch = Dispatch[{a -> b, b -> c, c -> a, d -> e, e -> d}]
Out[6]=
In[7]:=
ResourceFunction[
 "ReplaceAllOutside"][{a, f[b], c, f[d], e}, dispatch, _f]
Out[7]=

Use an Association for replacement rules:

In[8]:=
assoc = <|a -> b, b -> c, c -> a, d -> e, e -> d|>
Out[8]=
In[9]:=
ResourceFunction[
 "ReplaceAllOutside"][{a, f[b], c, f[d], e}, assoc, _f]
Out[9]=

Applications

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

In[10]:=
ResourceFunction[
 "ReplaceAllOutside"][{Subscript[f, 1][10], 2, 3, Subscript[x, 4], Subscript[f, 5][1, 2, 3]}, n_Integer :> N[n], _Subscript]
Out[10]=

Replace all non-held integers:

In[11]:=
ResourceFunction[
 "ReplaceAllOutside"][{1, Hold[2 + 2], 3, HoldPattern[4] :> 5, {6, HoldForm[7 + 8]}}, n_Integer :> Style[n + 1, Red], h_[___] /; MemberQ[Attributes[h], HoldFirst | HoldRest | HoldAll | HoldAllComplete]]
Out[11]=

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

In[12]:=
Short[data = EntityValue[{Entity["Species", "Species:FelisCatus"], Entity["Species", "Infraspecies:CanisLupusFamiliaris"], Entity["Species", "Class:Aves"]}, "PropertyAssociation"] // Normal // Flatten, 1]
Out[12]=
In[13]:=
ReverseSort[
 ResourceFunction["Proportions"][
  ResourceFunction["NestedLookup"][
   ResourceFunction["ReapAssociation"][
    ResourceFunction["ReplaceAllOutside"][data, s_String :> RuleCondition[
       Sow[s, "Strings"]], _Entity | _EntityProperty | _Quantity | \
_Missing]], {"Sown", "Strings"}]]]
Out[13]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information