HTML and CSS Reference
In-Depth Information
Regular Expressions
Manually inspecting and changing each file on even a small site is tedious and often cost-prohibitive. It is much
more effective to let the computer do the work by searching for mistakes and, when possible, automatically
fixing them. A number of tools support this, including command-line tools such as grep, egrep, and sed; text
editors such as jEdit, BBEdit, TextPad, and PSPad; and programming languages such as Java, Perl, and PHP. All
these tools provide a specialized search syntax known as regular expressions. Although there are small
differences from one tool to the next, the basic regular expression syntax is much the same.
For purposes of illustration, I'm going to use the jEdit text editor as my search and replace tool in this section. I
chose it because it provides pretty much all the features you need, it has a reasonable GUI, it's open source,
and it's written in Java, so it runs on essentially any platform you're likely to want. You can download a copy
from http://jedit.org/ .
However, the techniques I'm showing here are by no means limited to that one editor. In my work, I normally
use BBEdit instead because it has a slightly nicer interface. However, it's payware and only runs on the Mac.
There are numerous other choices. If you prefer a different program, by all means use it. What you'll need are
Full regular expression search and replace
The ability to recursively search a directory
The ability to filter the files you search
A tool that shows you what it has changed, but does not require you to manually approve each change
Automatic recognition of different character encodings and line-ending conventions
Any tool that meets these criteria should be sufficient.
Searching
The first goal of a regular expression is to find things that may be wrong. For example, I recently noticed that I
had mistyped some dates as 20066 instead of 2006 in one of my files. That's an error that's likely to have
happened more than once, so I checked for it by searching for that string.
In jEdit, you perform a multifile search using the Search/Search in Directory menu item. Selecting this menu
item brings up the dialog shown in Figure 2.6 . This is normally configured more or less as shown here.
The string you're searching for (the target string) goes in the first text field.
The string that will replace the target string goes in the second text field. Here I'm just going to find, not
replace, so I haven't entered a replacement string.
Search WWH ::




Custom Search