Pattern Searching


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

For example, if you want to find every usage of Mathematica code in your workspace that has an If with two arguments, you would go to the menu bar and choose Search > File, then choose the Mathematica Search tab. You could enter the pattern If[ _, _], which means an If with two arguments. This is shown below.

Pattern Searching Replacing

When you click Search this carries out the search and displays the results. A sample, which has two matches, is shown in the following.

Pattern Searching Replacing

You can click individual items in the search result and they will open the relevant source file and select the code that was found. An example is shown below.

Pattern Searching Replacing

Note how the search found the If statement even though there was a comment inside of it. In fact the search would have worked irrespective of formatting, comments, and the arguments of the target function. All of this could not be done purely with a textual search, even one with a nice regular expression system. The search can only work if it has access to a parsed form of the code, and this is exactly how the Workbench search operates.