Regular expressions are often used for performing document-wide find-and-replace actions. In some cases, skilled regular expression authors bravely use regular expressions to make changes across dozens or even hundreds of documents.
In this example, I will show you how to use regular expressions to replace a piece of content.
Imagine a scenario where you've been asked to update an older web page and replace a specific string of text that uses and a messaging "Hello world." You've been asked to:
- Replace the
tags with the more modern, compliance-friendly
tag.
- Substitute the word "world" and the closing period with your own name followed by an exclamation mark.
Imagine that the document you are presented looks something like this:
Untitled Document Hello world. Hello world. Hello world.
There are not one but three instances of the font tag and text in question.
In order to accomplish this task, I'll need to introduce a new concept: subexpressions.
Introducing Subexpressions
Regular expressions enable you to define subexpressions in order to refer later to certain fragments of your pattern. Subexpressions are defined using parentheses. A basic example of a subexpression would be a regular expression that looks like this:
(a)(b)(c)
This regular expression defines three subexpressions. The first expression pattern (a) can be referred to as the variable $1, the second (b) as $2, and the third (c) as $3. References to subexpressions are created sequentially beginning with 1.
Applying Subexpressions to the Problem
Now that I've explained what subexpressions are and how they can be used to reference pieces of an expression, you will use them to help solve the problem.
I've defined the following regular expression to match the text in the document.
(]*>)(Hello )(world.)()
First, notice that there are four subexpressions defined in this regular expression. The first subexpression matches the beginning tag ($1), the second matches "Hello " ($2), the third matches "world." ($3), and the fourth matches the closing
Complete the example now by performing the following steps:
- Create a new HTML document in Dreamweaver.
- Switch to Code view by selecting View > Code.
- Copy and paste the following three lines inside the
tag.
Hello world. Hello world. Hello world.
- Set the cursor to be at the first line of the document.
- Open the Find and Replace dialog box by selecting Edit > Find and Replace.
- Select the Use regular expressions check box to enable regular expression searches.
- Select the Match case check box.
- Select Current Document in the Find In pop-up menu.
- Select Source Code in the Search pop-up menu.
- Type the following regular expression in the Find text box:
(]*>)(Hello )(world.)()
- Click Find Next and verify that all three instances of the
tag are selected, one at a time. This verifies that the regular expression is in fact matching.
- Type the following regular expression in the Replace text box:
$2Rob!
In the Replace text box, you are indicating that you want to replace the entire font tag with a string that begins with the
tag, followed by the second subexpression of the regular expressions you're searching for, followed by my name and a closing
tag. In other words, you want to preserve the subexpression "Hello" and make changes before and after it (see Figure 12).
- Click the Replace All button.
In Code view, you should see that all three
tags have been removed and replaced with a newly constructed string that contains one fragment of the original string ("Hello").
No comments:
Post a Comment
Please share your thought about this