Using Regular Expressions for Searching


A regular expression is a pattern, written using special symbols, which describes one or more text strings. You use regular expressions to match patterns of text, so that Dreamweaver can easily recognize and manipulate that text. Like an arithmetic expression, you create a regular expression by using operators , in this case operators that work on text, rather than numbers .

The operators in a regular expression (see Table 4.5 ) are like the wildcard symbols that you may have seen in find and replace features in other programs, such as word processors, except that regular expressions are much more powerful. They can also be complex and difficult to learn and understand, so if Dreamweaver's other finding and replacing methods are sufficient for you, you may not need to bother with regular expressions.

Table 4.5.

Regular Expression Special Characters

C HARACTER

M ATCHES

\

Escape character; allows you to search for text containing one of the below special characters by preceding it with the backslash

^

Beginning of text or a line

$

End of text or a line

*

The preceding character zero or more times

+

The preceding character one or more times

?

The preceding character zero or one time

.

Any character except newline

\b

Word boundary (such as a space or carriage return)

\B

Non-word boundary

\d

Any digit 0 through 9 (same as [0-9])

\D

Any non-digit character

\f

Form feed

\n

Line feed

\r

Carriage return

\s

Any single white space character (same as [ \f\n\r\t\v])

\S

Any single non-white space character

\t

Tab

\w

Any letter, number, or the underscore (same as [a-zA-Z0-9_])

\W

Any character other than a letter, number, or underscore

[abcde]

A character set that matches any one of the enclosed characters

[^abcde]

A complemented or negated character set; one that does not match any of the enclosed characters

[a-e]

A character set that matches any one in the range of enclosed characters

[\b]

The literal backspace character (different from \b)

{n}

Exactly n occurrences of the previous character

{n,}

At least n occurrences of the previous character

{n,m}

Between n and m occurrences of the previous character

()

A grouping, which is also stored for later use

xy

Either x or y


Learning regular expressions is beyond the scope of this book, but we'll show you how to use one in an example. Let's say that you want to find all of the HTML comments throughout your site. You can use this simple regular expression:

 <!--[\w\W]*?--> 

Let's break that expression down. You read a regular expression from left to right. This one begins by matching the beginning characters of the HTML comment, <!-- . The square brackets indicate a range of characters; for example, [a-z] would match any character in the range from a to z. In this case, the range includes two regular expression operators: \w means "any single letter, number, or the underscore," and \W means "any character other than a letter, number, or underscore." Taken together as a range, [\w\W] means "any range of characters." The * means "the preceding character (in this case, everything found by the contents of the square brackets) zero or more times" and the ? means "the preceding character zero or one time." Taken together, they match a comment of any length. The regular expression ends by matching the closing characters of an HTML comment, --> .

To search with a regular expression:

1.
Choose Edit > Find and Replace, or press Ctrl-F (Cmd-F).

The Find and Replace dialog appears.

2.
From the Find in pop-up menu, choose one of the options for the scope of your search.

3.
From the Search pop-up menu, choose any of the options.

In this case, since we're looking for HTML comments, you should choose Source Code.

4.
Enter the regular expression in the Find field ( Figure 4.45 ).

Figure 4.45. You can add regular expressions to both the Find and Replace fields.


5.
Select the check box next to "Use regular expressions."

When you choose "Use regular expressions," it disables the "Ignore whitespace" search option, because they are mutually exclusive.

6.
(Optional) Enter text or a regular expression in the Replace field.

7.
Depending on what you want to do, click Find Next, Find All, Replace, or Replace All.

Tip

  • There's a lot to say about regular expressions, certainly enough to fill an entire book or twelve. If you're interested in learning more, check out Mastering Regular Expressions , by Jeffrey Friedl (2002, O'Reilly & Associates). For a lighter read, try Teach Yourself Regular Expressions in 10 Minutes by Ben Forta (2004, Sams). There are also many Web sites that provide regular expression tutorials, which you can find with a Google search.





Macromedia Dreamweaver 8 for Windows & Macintosh Visual QuickStart Guide
Macromedia Dreamweaver 8 for Windows & Macintosh
ISBN: 0321350278
EAN: 2147483647
Year: 2005
Pages: 239

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net