Global Find and Replace

Conducting a site-wide search and replace is easy using the Global Find and Replace feature. You can preview your plain text or regular expression matches and revert your most recent change if you make a mistake. Global Find and Replace is an excellent tool for administrators with level 10 access privileges.

Global find and replace is non-discriminatory in the files it selects on the staging server. This tool affects all files on the staging server, regardless of whether those files are checked out, and may affect new page templates if the templates folder is included in the directory scope selection.

CautionGlobal Find and Replace can be a site-wide operation. Use caution and make a backup, if necessary. Review the Global Find and Replace documentation before using this feature for the first time.

 

1.1  Locating the Global Find and Replace Feature

Global Find and Replace is found in the Content tab under "Find/Replace" (see  Figure 1.1 below).

Locating Global Find and Replace

Figure 1.1: Locating Global Find and Replace

1.2  Caution Statement

This feature makes global changes to your website inside the directories you select. Although you may revert your last replace operation if you make a mistake, in general, it is a good idea to be vigilant while conducting any global, site-wide operations.

Review the change summary after submitting a replace request. If anything is out of the ordinary, revert your replacement operation and refine your search pattern. Always use the Find button to preview your search matches before submitting a replace. Remember, only the most recent find/replace can be reverted.

 

 

 

2. Literal Text Searches

The simplest application of a find/replace tool is the literal text search. This allows you to update or find instances of a recurrent character string with ease, searching only within specific directories you select.

2.1  Find/Replace Window

The Find/Replace window, shown in Figure 2.1 below, presents a few options.

Find/Replace window

Figure 2.1: Find/Replace window. (1) Search within a selected directory. (2) Select your search type, either using literal text or a regular expression. (3) Specify your find and replace criteria. (4) Choose whether to conduct a simple find operation and display the matches, perform a find/replace operation, or reset the form to its original state.

First, select the directory in which you would like to search. Browsing inside a directory will allow the selection of a specific subdirectory. If no directory is selected, only files within the current directory are searched.

If the top checkbox is selected, located above the directory listing, Global Find and Replace will operate inside all the folders shown in the list. Checkmarking specific folders will limit the search scope to those directories and their subdirectories. In other words, checkmarking a folder will allow Global Find and Replace to affect that directory and anything below it, but files located above the current position in the directory tree will remain unaffected. An example is shown in the following figure:

2 directories selected No Directories Selected All directories selected
(a) (b) (c)

Figure 2.2: Directory scope example from left to right: (a) search inside the directories ./president/ and ./vice_president/ and their subdirectories, (b) search files located inside the current directory only, and (c) search all directories and subdirectories below this location.

Next, select the search type. Two options are available: Literal Text and Regex (regular expression). Regular expression searches are covered in Section 3. For this purpose, it is assumed that Literal Text has been selected.

Finally, input the character string for which you wish to search. For instance, enter "bob@yourwebsite.com" to search for Bob's email address. Click on the Find button at the bottom of the screen to display the location of Bob's email address within all files contained in the selected directory and its subdirectories.

2.2 Performing a Literal Find and Replace

Referring back to the example in Section 2.1, Bob's email is to be replaced with "robert@yourwebsite.com". Type the new email address into the Replace field, assuming the Find field is set to Bob's old email address ("bob@yourwebsite.com").  Click the Find/Replace button and all occurrences of Bob's old email address will be replaced with the new one. You will then see a report detailing every change that was made to your files and its context (surrounding text). Don't worry if you make a mistake after a replace operation. You can always revert the most recent find/replace (see Section 4).

Specifying a comment will attach a short note about your save in the OU Campus file system for future reference. These comments are useful in tracking file changes long after your replace operation has occurred. Pressing the Reset button is a quick way to clear the form and start fresh!

You must publish your files to make any changes to the live server. This can be done within Global Find and Replace by clicking the publish button after performing a replace operation (see figure 4.2). Files can also be published individually in the content tab (pages view) outside of this tool.

2.3 Display Limits

Conducting a find operation will display each match within its surrounding context. Up to 100 items per page, with a maximum of 10000 matches total, will be displayed.

These display limits apply to the find function only. Performing a replace will affect all matches, above and beyond the preview display limits.

 

 

 

3. Regular Expression Searches

The Global Find and Replace search pattern is not limited to plain text. You can match any character combination using regular expressions.

3.1 Common Example

Regular expressions are useful to programmatically define your find and replace matches. The example in Figure 3.1 below illustrates how to use back references ($1) to replace a specific <span> tag with an <h1> tag while preserving the content in between the tags. A back reference is created by the capturing group, shown by the parentheses, and is designated in the Replace field by the dollar sign and the capturing group number.

Example of using a back reference with Global Find and Replace

Figure 3.1: Example of using a back reference with Global Find and Replace

3.2 Supported Regular Expressions

The following list outlines the regular expressions syntax supported by Global Find and Replace:

Characters
x The character x
\\ The backslash character
\0 n The character with octal value 0 n (0 <= n <= 7)
\0 nn The character with octal value 0 nn (0 <= n <= 7)
\0 mnn The character with octal value 0 mnn (0 <= m <= 3, 0 <= n <= 7)
\x hh The character with hexadecimal value 0x hh
\u hhhh The character with hexadecimal value 0x hhhh
\t The tab character ('\u0009')
\n The newline (line feed) character ('\u000A')
\r The carriage-return character ('\u000D')
\f The form-feed character ('\u000C')
\a The alert (bell) character ('\u0007')
\e The escape character ('\u001B')
\cx The control character corresponding to x

Character classes
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]]
a through z, and not m through p: [a-lq-z](subtraction)

Predefined character classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]

POSIX character classes (US-ASCII only)
\p{Lower} A lower-case alphabetic character: [a-z]
\p{Upper} An upper-case alphabetic character:[A-Z]
\p{ASCII} All ASCII:[\x00-\x7F]
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit} A decimal digit: [0-9]
\p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_'{|}~
\p{Graph} A visible character: [\p{Alnum}\p{Punct}]
\p{Print} A printable character: [\p{Graph}]
\p{Blank} A space or a tab: [ \t]
\p{Cntrl} A control character: [\x00-\x1F\x7F]
\p{XDigit} A hexadecimal digit: [0-9a-fA-F]
\p{Space} A whitespace character: [ \t\n\x0B\f\r]

Boundary matchers
^ The beginning of a line
$ The end of a line
\b A word boundary
\B A non-word boundary
\A The beginning of the input
\G The end of the previous match
\Z The end of the input but for the final terminator, if any
\z The end of the input

Greedy quantifiers
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times

Reluctant quantifiers
X?? X, once or not at all
X*? X, zero or more times
X+? X, one or more times
X{n}? X, exactly n times
X{n,}? X, at least n times
X{n,m}? X, at least n but not more than m times

Possessive quantifiers
X?+ X, once or not at all
X*+ X, zero or more times
X++ X, one or more times
X{n}+ X, exactly n times
X{n,}+ X, at least n times
X{n,m}+ X, at least n but not more than m times

Logical operators
XY X followed by Y
X|Y Either X or Y
(X) X, as a capturing group

Back references
$n The contents of the nth capturing group will be used in the replace field

Special constructs (non-capturing)
(?:X) X, as a non-capturing group
(?=X) X, via zero-width positive lookahead
(?!X) X, via zero-width negative lookahead
(?<=X) X, via zero-width positive lookbehind
(?<!X) X, via zero-width negative lookbehind
(?>X) X, as an independent, non-capturing group

Source: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#sum

3.3  Case Insensitive, Multiline, and Dotall Matches

To modify the case sensitivity, multiline, or dotall behavior, use the following preprocessing instructions as a prefix to your regular expression:

Preprocessing
Instruction

Description

Example

(?i)

Case insensitive
matching

(?i)ANY case YOU want

(?m)

Multiline matching

(?m)^search from the beginning of a text line to the end$

(?s)

Dotall matching
(dot matches everything)

(?s)<div>(.*?)</div>

(?sim)

Use all three!

Table 3.1: Preprocessing instructions

 

 

4. Last Find/Replace

To undo a global replacement, you can revert the latest replace operation using the Last Find/Replace screen.

4.1 Locating Last Find/Replace

Access this screen by clicking on the Last Find/Replace link, as shown in Figure 4.1 below.

Locating the Last Find/Replace link

Figure 4.1: Locating the Last Find/Replace link

4.2 Revert and Publish

After clicking on the Last Find/Replace link, a window will open with information about your last find/replace (see Figure 4.2 below). Two options are available in this window: Revert and Publish.

Last Find/Replace window

Figure 4.2: Last Find/Replace window

Selecting Revert will undo the most recent replace operation. Selecting Publish will push all the changed files to the live server. Selecting Cancel returns you to the find/replace screen.

 

 

5. Best Practices

Sometimes it's best to skip the learning experience. The best practice tips in this section outline some useful tidbits of knowledge that should help ease you into using Global Find and Replace without experiencing any unwanted mishaps.

5.1 Backup Your Website Beforehand

Creating backups is smart. Creating a site-wide backup before conducting a global find/replace operation is even smarter. Administrators with level 10 access can export a copy of the OU Campus staging server using the Export button located in the site setup panel (see Figure 5.1 below).

Location of the Export button within the site setup panel

Figure 5.1: Location of the Export button within the site setup panel

If you have direct FTP access to the staging server, you may be able to restore a backup yourself by simply uploading these files to the staging server and publishing your website in the site setup panel. There is no import function.

5.2 Use the Find/Replace Comment Field

The Global Find and Replace comment field is used to attach a quick note of your changes within the OU Campus file system. When reverting individual pages using the versioning system, a record is kept explaining the changes that were made to that file. This comment will appear in the page publish note, which is useful in tracking your recent file publishes.