Pattern Find and Replace


Pattern find and replace lets you find certain structures in your code and then modify them. It does not use a textual search, but works on the actual code structure itself.

For example, you could change your code so that things which looked like

If[ x, y = expr]

got changed to look like

If[ x, y = {expr^2}]

You would open the Source popup menu in the Mathematica source editor by right-clicking and choosing Source > Expression Find/Replace. You would then fill in the dialog as follows.

Pattern Searching Replacing

When you click Preview this allows you to preview all the changes that were introduced. From this you can accept or reject the changes. When they are accepted the changes are made.

Before the transformation some of the code might look as follows.

Pattern Searching Replacing

After the transformation it has changed to the following.

Pattern Searching Replacing

Note that as the transformation was carried out, a comment in the code was preserved, as well as the original formatting. This technology is a powerful way to make modifications to your source code.