Searching and Replacing with Regular Expressions


Regular expressions are control characters that describe character combinations or patterns in text. For example, if you want to find all occurrences of years from 17001799, the pattern is "17" followed by any combination of two numbers from 09. You can use a number of special characters to define the search pattern. For example, the backslash (\), dollar sign ($), and question mark (?) are all special characters. When using regular expressions, it is important to know these characters; if you are looking for such special characters in your text, you need to precede the character with a backslash to indicate that it is part of the character search and not used as a special character.

Appendix A contains a table with all the special characters, regular expressions, and their meanings.

In this exercise, you will use patterned searches in a document.

1.

Open the workshops.html file in the Lesson_17_FindReplace/schedule folder, place the insertion point at the beginning of the document and choose Edit > Find and Replace.

The Find and Replace dialog box opens.

2.

Select Current Document in the Find In menu. Set the Search menu to Text. Check the Use Regular Expression option box.

Notice that Ignore Whitespace is disabled or dimmed when you select Use regular expression.

Note

The Ignore Whitespace option, when selected, treats all whitespace as a single space for the purposes of matching. For example, with this option selected, "this text" matches "this text" but not "thistext." The Ignore Whitespace option is not available when the Use Regular Expression option is selected; you must explicitly write your regular expression to ignore whitespace. Note that p and br tags do not count as whitespace.

3.

In the Find text field, type \d*:\d\dam.

Be sure to type backslashes (\), not forward slashes (/), when you enter \d*:\d\dam into the text field.

In this exercise, you will apply emphasis styling to workshops starting before 12:00 pm to draw attention to them. The search pattern you defined in Step 3 uses the special character \d, which represents any digit (09). An asterisk means "find zero or more of the previous character," so \d* looks for zero or more digits. Thus this pattern searches for zero or more numbers, followed by a colon, followed by exactly two digits and the text "am." This will match any time in the morning, such as 11:30 am. See Appendix A for more regular expressions to use for search patterns.

Note

To distinguish decimal numbers from years19.09 for examplewhen searching in the document, include a period in your search. A period is also a special character, so you need to precede it with a backslash in your search.

4.

Click the Find Next button.

Dreamweaver selects "11:00am" by the entry for October 20. You might need to move the Find and Replace dialog box to see the selection.

5.

Continue to click the Find Next button several times to see what gets selected.

There are two workshops that begin before 12:00 pm.

6.

In the Find and Replace dialog box, change the Search menu selection to Source Code.

In the following steps, you use the emphasis tags (<em> and </em>) to make the times bold. However, if you were to type <em>\d*:\d\dam</em> in the Replace text field, the text in the Document window would be changed to \d*:\d\damit would literally change the numbers in the time. Instead, you need to isolate the search as a pattern by surrounding the pattern that you are searching for in parentheses.

7.

In the Find text field, insert a left parenthesis before the text and a right parenthesis after the text, like this: (\d*:\d\dam).

The parentheses create the first pattern.

8.

In the Replace text field, type <em>$1</em>.

To reference the pattern you created in the previous step, you use $1 in the Replace text field. Surrounding this symbol with emphasis tags causes the same tags to be added around the results of the first pattern search. If you were to create several patterns, the next pattern, as indicated by the parentheses, would be referenced in this text field with $2, and so on in a sequential manner.

9.

Click the Find Next button and then click the Replace button.

The emphasis tags are placed around the time in the Code inspector. Click in the Document window to see the results.

10.

Click the Replace All button to find and replace all occurrences in the document. Save the workshops.html document. Click the context menu in the upper-right corner of the Results panel and choose Clear Results. Close (Macintosh) or collapse (Windows) the Results panel.

Tip

To view and clear the results, Macintosh users need to open the Results panel if it is not already open by choosing Window > Results.


Close the Results panel but leave the workshops.html file open for the next exercise.




Macromedia Dreamweaver 8(c) Training from the Source
Macromedia Dreamweaver 8: Training from the Source
ISBN: 0321336267
EAN: 2147483647
Year: 2006
Pages: 326

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