Advanced Grep Topics
After matching all 6 digits and then failing to match “foo”, the normal action of the grep
engine is to try again with only 5 digits matching the \d+ item, and then with 4, and so on,
before ultimately failing. Once-only subpatterns provide the means for specifying that once
a portion of the pattern has matched, it is not to be reevaluated in this way, so the matcher
would give up immediately on failing to match “foo” the first time. The notation is another
kind of special parenthesis, starting with “(?>”, as in this example:
This kind of parentheses “locks up” the part of the pattern it contains once it has matched,
and a failure further into the pattern is prevented from backtracking into it. Backtracking
past it to previous items, however, works as normal.
In most situations, such as in the example above, the time saved by using once-only
subpatterns is insignificant—a few small fractions of a second, at most. With some
complicated grep patterns or with humongous lines of text, however, you can save
tremendous amounts of time using once-only subpatterns.
Once-only subpatterns are not capturing subpatterns. Simple cases such as the above
example can be thought of as a maximizing repeat that must swallow everything it can. So,
while both \d+ and \d+? are prepared to adjust the number of digits they match in order to
make the rest of the pattern match, (?>\d+) can only match an entire sequence of digits.
Once-only subpatterns can be used in conjunction with lookbehind assertions to specify
efficient matching at the end of a line of text. Consider a simple pattern such as:
when applied to a long line of text which does not match (in other words, a long line of text
that does not end with “abcd”). Because matching proceeds from left to right, the grep
engine will look for each “a” in the subject and then see if what follows matches the rest of
the pattern. If the pattern is specified as:
the initial .* matches the entire line at first, but when this fails (because there is no
following “a”), it backtracks to match all but the last character, then all but the last two
characters, and so on. Once again the search for “a” covers the entire string, from right to
left, so we are no better off. However, if the pattern is written as:
there can be no backtracking for the .* item; it can match only the entire line. The
subsequent lookbehind assertion does a single test on the last four characters. If it fails, the
whole match fails immediately. For long strings, this approach makes a significant
difference to the processing time.
When a pattern contains an unlimited repeat inside a subpattern that can itself be repeated
an unlimited number of times, the use of a once-only subpattern is the only way to avoid
some failing matches taking a very long time (literally millions or even billions of years, in
some cases!). The pattern:
Chapter 8: Searching with Grep
matches an unlimited number of substrings that either consist of non-digits, or digits
enclosed in <>, followed by either ! or ?. When it matches, it runs quickly. However, if it is
attempts to match this line of text:
it takes a long time before reporting failure. So long, in fact, that it will effectively “freeze”
TextWrangler. This is not really a crash, per se, but left to run on its own, it might take
years before it finally fails. (We are not sure, frankly, because much like determining how
many licks it takes to get to the center of a Tootsie Pop, we do not feel like waiting long
enough to find out.)
The reason this takes so long to fail is because the string can be divided between the two
repeats in a large number of ways, and all have to be tried before the grep engine knows for
certain that the pattern will not match. (The example used [!?] rather than a single character
at the end, because both PCRE and Perl have an optimization that allows for fast failure
when a single character is used. They remember the last single character that is required for
a match, and fail early if it is not present in the string.) If the pattern is changed to
sequences of non-digits cannot be broken, and failure happens quickly.
Consider the problem of matching a string in parentheses, allowing for unlimited nested,
balanced parentheses. Without the use of recursion, the best that can be done is to use a
pattern that matches up to some fixed depth of nesting. It is not possible to handle an
arbitrary nesting depth. Perl 5.6 has provided an experimental facility that allows regular
expressions to recurse (among other things). It does this by interpolating Perl code in the
expression at run time, and the code can refer to the expression itself. Obviously,
TextWrangler’s grep engine cannot support the interpolation of Perl code. Instead, the
special item (?R) is provided for the specific case of recursion. The following recursive
pattern solves the parentheses problem:
First it matches an opening parenthesis. Then it matches any number of substrings which
can either be a sequence of non-parentheses, or a recursive match of the pattern itself (that
is, a correctly parenthesized substring). Finally there is a closing parenthesis.
This particular example pattern contains nested unlimited repeats, and so the use of a once-
only subpattern for matching strings of non-parentheses is important when applying the
pattern to strings that do not match. For example, when it tries to match against this line of
it yields “no match” quickly. However, if a once-only subpattern is not used, the match runs
for a very long time indeed because there are so many different ways the + and * repeats
can carve up the subject, and all have to be tested before failure can be reported.
Recommended Books and Resources
Recommended Books and
Mastering Regular Expressions, 3rd Edition
by Jeffrey E.F. Friedl. O’Reilly & Associates, 2006. ISBN 0-596-52812-4
Although it does not cover TextWrangler’s grep features specifically, Mastering Regular
Expressions is an outstanding resource for learning the “how-to” of writing useful grep
patterns, and the second edition is even better than the original.
The TextWrangler Talk discussion group covers a wide range of topics and questions about
using TextWrangler, which frequently include searching and the use of grep patterns.
TextWrangler’s grep engine is based on the PCRE library package, which is open
source software, written by Philip Hazel, and copyright 1997-2004 by the University of
Cambridge, England. For details, see: http://www.pcre.org/
Chapter 8: Searching with Grep
CHAP TE R
Browsers are special kinds of windows that let you see a lot of information about
files at once. Browsers typically have two panes: one pane lets you select a file,
the other displays detailed information about the file (often its contents). If you
have performed a Find All search, you have already seen an example of a
In this chapter
Browser Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
List Pane – 165 • Toolbar – 166
Text View Pane – 166 • Splitter – 166
Disk Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Disk Browser Controls – 167
Contextual Menu Commands – 168
Dragging Items – 168
Using the List Pane in Disk Browsers – 168
Search Results Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Error Results Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
All TextWrangler browsers share the same basic structure and behavior. All
browsers have a toolbar, a file list, and a text pane. You can either edit files
directly in any browser window, or open them separately.
The top pane of a browser lists the items available in the browser. This pane
shows different information for different kinds of browsers:
You can open both files and folders from the list pane. When you double-click a
folder name, TextWrangler replaces the file list pane with the contents of the
folder. When you double-click a file name, TextWrangler opens the file in an
editing window. If the file list pane also included a line number, TextWrangler
scrolls to that line.
File List pane contains
Files and folders that TextWrangler can open
File and line number of each match
Error results (or)
File, line number, and status message for each
Chapter 9: Browsers
Controls above the list may allow you to determine what kinds of items are displayed in the
list. For example, in disk browsers, there is a popup menu that lets you choose to display
text files, all files, or other types of files, and another that lets you return the browser to a
parent directory of the current folder. In error browsers, checkboxes allow you to hide or
show all errors, warnings, or notes.
For results browsers, TextWrangler shows a hierarchical listing, where all the results
associated with a particular file are grouped under that file, using disclosure triangles
similar to those in the Finder’s list views to reveal or hide the results list.
To remove items from the display list, select them and press the Delete key, or choose Clear
from the Edit menu.
In results browsers, you may Control-click on items in the list to bring up the contextual
menu with relevant commands, such as “Copy”.
The browser toolbar is like the toolbar in editing windows. Some browsers have additional
buttons and controls in the status area as well.
These standard items—the pencil icon; the Function, Text Options, Mark, Path popup
menus; and the Info buttons—should already be familiar to you, since they appear on
TextWrangler document windows by default. See “Window Anatomy” in Chapter 4 for an
explanation of these standard TextWrangler functions.
Text View Pane
When you click on a file name in the list pane, TextWrangler displays that file in the text
view pane, and you can edit the file just as if it were open in a document window.
You can change the size of the file list pane or the text view pane by dragging the double
line that separates them. Double-clicking on the splitter bar will collapse the text view pane
completely, and double-clicking on it again (in the bottom of the browser window) will
restore the text pane to its previous proportions. You can also choose the Hide Editor or
View Editor commands in the View menu to hide or display the text view pane.
Use a disk browser to explore the contents of a disk or a folder without opening each file
one at a time.
To open a disk browser, pull down the File menu and choose Disk Browser from the New
submenu. TextWrangler opens a new disk browser that starts in your home directory, but
you can navigate to any desired location:
The name and path of the file (if any) and directory currently being viewed are displayed in
the title bar of the window. The file list pane displays all the items in the current folder.
Click on a file in the file list pane to open it in the text pane, or double-click to open the file
into a text window.
Disk Browser Controls
The menus at the top and bottom of the file list pane let you create new files and folders,
open existing files and folders, reveal them in the Finder or navigate to them in the
Terminal, limit the kinds of files to show in the list pane, and navigate through your disks
The Directory popup menu at the top of the file list pane always shows the currently active
folder. You can use this menu to “back out” of any folder you are currently in to a higher-
level folder (as you can by Command-clicking the name of a folder in the Finder).
Chapter 9: Browsers
The commands on the Action (gear) popup menu at the bottom of the file list pane allow
you to open the selected items, reveal them in the Finder, copy their paths, navigate to their
location in the Terminal, move them to the Trash, or create a new file or folder.
The Filter (magnifying glass) popup menu at the bottom of the file list pane lets you specify
what kinds of files TextWrangler should display:
•All Available: All files which TextWrangler recognizes, including its own
document types. This includes text files, images, text factories, and so on.
•Text Files Only: Only files which TextWrangler recognizes as text files.
•Everything: All items present, including invisible files and folders.
You can also select a file filter to further limit what files TextWrangler should display. (You
can define additional file filters in the Filters panel of the Setup window.)
Toggle Editor Button
Click this button to collapse or expand the browser’s text view pane. (This button has the
same effect as choosing the View/Hide Editor command in the View menu.)
Contextual Menu Commands
If you select one or more items in the file list pane and bring up the contextual menu,
TextWrangler will offer a variety of commands including those available from the Action
You can select and drag files and folders from a disk browser’s file list to any location,
either within TextWrangler or elsewhere, which can accept file or folder drags. For
example, you can drag a file from a disk browser to an editing window to insert its
contents, or to a folder in the Finder to copy or move it.
Using the List Pane in Disk Browsers
The list pane of a disk browser displays disks, files, and folders. When you are at the
computer level, the list shows all mounted volumes.
When you click a folder or disk in the list pane, TextWrangler displays the names of all the
files it can open in the text pane, subject to the criteria specified by the Show and Filter
When you click a file name in the list pane, TextWrangler displays that file in the text pane.
To open a folder or disk and display its contents in the file list pane, you can either double-
click it, or Select it and press Command-Down Arrow.
To go up one level to the enclosing folder or disk, you can either choose the enclosing
folder from the directory popup menu, or press Command-Up Arrow
You can also use Quick Look to examine any non-text file by selecting it and pressing the
Search Results Browsers
When the list pane has input focus, the browser window’s AppleScript “selection”
property will return a list of the files currently selected. See “Getting and Setting
Properties” on page 212 for further details.
Search Results Browsers
If you selected the Batch Find option when performing a multi-file search, TextWrangler
displays every occurrence of the search string in the searched files in a search results
The items at the top of the window tell you how many matches TextWrangler found in the
set of files you specified, as well as whether any error conditions or warnings were
generated during the search. The list pane lists each line that contains the matched text.
Every match is identified by file and line number. To choose whether to display the search
errors, warnings, and results, use the checkboxes at the top of the browser.
To open the file which contains a particular match, just click on that match in the results
list. After you have opened a file, you can use the Search menu commands to continue
searching it. (See Chapter 7 for more information on searching.)
The Open button opens the selected items using TextWrangler. To open the selected items
using the Finder, hold down the Option key while clicking the Open button.
Chapter 9: Browsers
Error Results Browsers
When you check the syntax of a Unix script, or run a script which generates any errors,
TextWrangler will open an error results browser to list those errors.
Each entry in the list pane corresponds to an error, warning, or note. You can use the
checkboxes for each type of item to suppress or display the associated results as desired.
If you click on a entry in the file list, TextWrangler will open the corresponding file in the
text display pane and select the section of text related to the error.
Documents you may be interested
Documents you may be interested