Search & Replace

WebDesign provides advanced search and replace features including the ability to match entire words only, case sensitive options and the ability to search exclusively in the selected text. It also offers multi-file search and replace features and powerful Regular Expression searching.

This section will get you familiar with all of WebDesign's search and replace features as well as provide you with an overview of how to use its Regular Expression searching.


Overview

WebDesign provides many options to help narrow down your search and find or replace only the text that needs to be.

Start From Top

Begins the search at the beginning of the document. If 'Start From Top' is not selected, the search starts at the current position of the insertion point.

In Selection

If you only want to search within a portion of your document or replace only the words that appear in a certain section, select the text you want to search and choose the 'In Selection' option. When selected, WebDesign will only search and replace within the bounds of the selection. This option can not be selected in conjunction with the 'Start From Top' option.

Extended Selection

If you need to replace text from one point to the end of a certain word or pattern (using regular expression searching), the 'Extended Selection' option will select the text beginning at the insertion point to the end of the search string. When replacing, all the text from the insertion point to the end of the search string will be replaced with the replace string.

Case Sensitive

When searching documents, it may be important to treat 'A' and 'a' as different characters along with every other capital and lowercase letter. 'Case Sensitive' searching will do just that and match only strings or patterns that match the exact same word taking case into account.

Match Entire Word

You may require your search string to only match complete words and not just smaller parts of a larger words. The 'Match Entire Word' option will only find whole words and not just the search string within a word.

Use Grep

Enables Regular Expression searching which allows searching for patterns in your document instead of just plain words. See below for further details included syntax rules and examples

If you only need to search or replace certain portions of your document you can use WebDesign's condense text feature to condense multiple portions of your document and exclude them from your search. Select a certain section of your document and select Condense Selected Text from the Text menu. You can extend condensed text by double clicking it or selected Extend All Condensed Text from the Text menu. It may also be useful to exclude HTML tags from the search and replace by selecting Condense All HTML Tags from the HTML Tools menu.

You can search for the previous string entered in the find and replace dialog using the menu items and equivalent command keys under the Text menu. You may choose to Find Again (Command-G), Replace Next (Command-=) or Replace Next and Find Again (Command-T).

[Top]


Multi-file search & replace

If you need to find or replace certain text in a folder or even an entire web site, such as copyright info, WebDesign allows you to search entire folders and sub folders instantly. WebDesign can quickly open all documents containing certain text or quickly do a replace all for every document in an entire web site. Other options allow the ability to find and replace text in all documents currently opened in WebDesign.

To search an entire folder or web site, select the 'Multi File Search' option. Then, select a folder to search by pressing the 'Select...' button. Navigate to the correct folder and press OK. If you only want to search in web site files which include almost any regular text file, select the 'Exclude Non Web Site Files'. The 'Keep Documents Opened' option will keep all documents, even documents who do not contain the search string opened. When replacing all, this option will also keep all documents opened if no search string was replaced in the file.

Searching in all open documents simply finds all, or replaces all, in all documents currently opened by WebDesign.

Almost all of WebDesign's find and replace options can be applied when searching multiple files including matching entire words and case sensitive searching. Regular Expression searching can also be used for multi-file search and replace by selecting the 'Use Grep' option.

[Top]


Using Regular Expressions

If you need to find certain patterns in your documents such as an HTML tag, specific tags containing certain attributes, words containing only certain letters, etc..., then you can take advantage of WebDesign's Regular Expression searching.

Regular Expression (or Grep) allows you to search documents using search patterns and setting conditions that must be met, instead of actual words. WebDesign's Regular Expressions syntax is almost identical to that of the perl's Regular Expression syntax and BBedit's Grep syntax.

The WebDesign manual gives an overview of the proper syntax to use but does not provide in depth examples or explanations for most syntax rules. Regular Expression is a large topic and couldn't possibly be fully covered in this manual.

Most Characters Match Themselves

The first thing to remember is that most characters match themselves. So for example, 'a' would find all occurrences of 'a' (and 'A' if not using case sensitive searching) and 'hello' would match all occurrences of 'hello'.

Special characters, which are used to represent other things such as the beginning of a line, line feeds etc... will not match themselves when entered in the find field. To search for a special character in your document it must be preceded by a backslash ('\'). So if you would like to search your document for an asteriks ('*'), which normally would specify how many times a pattern should repeat, you would have to enter '\*' into the find field of the find dialog. And to search for a backslash you would have to enter '\\'.

Wildcard Characters

The following characters are used to match certain characters that are in the specified position.

Character Description Example
. Any single character, including spaces except a line break  
^ Search string must begin at the beginning of a line (unless used in a character class as seen below) ^hello - Find a line that begins with hello
$ Search string must be at the end of a line (unless used in a character class as seen below) hello$ - finds a string that ends with hello

To search for a line only containing certain text you could set your search string to something like this '^line only containing this text and nothing more$'

Note that when using the '^' or '$' meta character it will not return an extra character. So when searching for 'hello$', if it is found, only hello will be returned, not the line break character. If you want the line breaking character to be returned, you would search for 'hello\r'.

More Positional Characters

The following characters are similar to the above wildcard characters and can specify where a word must be positioned in your text in order for a match to be returned.

Character Description
\A Returns a match if the search string is contained at the beginning of the document unlike '^' which will return a match at the beginning of the document or at the beginning of each line.
\b Matches any word boundary
\B Matches a non-word boundary
\z Returns a match if the search string is contained at the end of the document unlike '$' which will return a match at the end of the document or at the end of each line.
\Z Returns a match if the search string is at the end of the document or before a trailing carriage return if there is one

A search string of \bgood\b will match a source string of 'this is good.' where good would be returned. However it will not return any match if the source string is 'Have a goodnight.'.

Character Classes

Character classes allow you to search for words containing certain characters or that do not contain certain characters. The set of characters are inclosed in brackets and if you choose to ignore the specified characters in the group you must precede the character list with a '^' right after the opening bracket. Here are some examples;

Character Class Description
[aeiou] Will return a match if the source string contains an 'a', 'e', 'i', 'o', or 'u'
[^aeiou] Matches any character not listed
[a-zA-Z0-9] Matches any letter or number. Note; case sensitivity is controlled in the find dialog
[^aeiou0-9] Matches any character except vowels or digits

You can match '[' and ']' characters by placing them right after the opening bracket like so '[[]' and '[]]'.

You can match the carrot '^' character by placing it at anywhere except for at the beginning of the character set like so '[aeiou^]' or [ae^iou]' will match any vowel or the carrot '^'. And to search for a hyphen '-' you must place the hyphen at the beginning of the character set like so '[-aeiou]' which will match any vowel or a hyphen. You can also include any of these characters by escaping them using a back slash '\' as discussed above.

A character class matches individual characters listed within the brackets. It will not match a word that contains those characters in that order. So for example '[phone]' will not return a match for 'phone' but will only return a match for 'p', 'h', 'o', 'n' or 'e'.

Non-Printing Characters

WebDesign's Regular Expression searching allows you to search for non-printing characters as well as search for characters by their hexidecimal or octal value.

Character Description
\n Matches a newline
\r Matches a return
\t Matches a tab
\f Matches a form feed
\nnn Matches an ASCII character of the specified octal code
\xnn Matches an ASCII character of the specified hexidecimal code
\cX Matches an ASCII control character
\metachar Matches the meta-character (\, ., |)

If you want to search for a line break within a certain search string you can search for 'This sentence\rshould appear on different lines'. This will return a match if the sentence is broken up onto two lines after 'sentence'. Using \r is useful if you want to search for a string containing a line break as shown in the example. You can use '^' and '$' to match search strings beginning or ending at a line respectively.

Other Special Characters

The following patterns are wildcards for the specified characters.

Character Description
\d Matches a digit. Same As [0-9]
\D Matches a non-digit. Same As [^0-9]
\w Matches an alphanumeric character (a-z, A-Z, 0-9 and '_')
\W Matches a non-word character. Anything that \w won't match
\s Matches a white space (space, tab, newline, etc...)
\S Matches a non-white space character

Repetition Characters

Repetition characters allow you to repeat specified patterns. They can be used with any of the search patterns discussed above by replacing x with the pattern.

Character Description
x? Matches 0 or 1 x's
x* Matches 0 or more x's
x+ Matches 1 or more x's
x{num} Matches num x's where num is an integer value
x{min,} Matches atleast min x's
x{min,max} Matches atleast min x's but no more than max

The repitition characters '*' or '?' can match 0 occurrences meaning that a match can be found but nothing will be selected in your document if the pattern is not present. Since this is the case, if you want to match more than one occurrence your should replace '*' with '+'.

Combining and Creating Patterns

Regular Expressions allow you to combine the syntax rules above to create specialized patterns to match words, sentences, lines etc...which match certain conditions. So far, the above syntax rules have only really discussed searching for individual characters. The following examples show you how to combine these above rules to create more complex patterns.

Notice that patterns can be as simple as 'webdesign' which will return a match if those characters, in that order, are found in your document.

Pattern Matches Examples
[0-9]+\s\d+ Matches any number of digits, then a space followed by any number of digits after the space 1234 7
5486 2536
\d{4}[\t ]A\.D\. Matches 4 digits followed by a space or tab and then A.D. 1995 A.D.
1235 A.D.
^\w+\s Matches the first word of each line followed by a space This is a line of text
If this was a line, the underlined text would be matched
\w+[ed]\.\r Matches a word ending in 'e' or 'd' at the end of a sentence followed by a carriage return Underlined text is matched here.

Provided there is a carriage return at the end.
Alternation Operator

If you want to match any number of patterns you can use the alternation operator ('or' operator), '|'. This will allow you to combine more than one pattern and WebDesign will find either pattern on either sides of the '|' character. You can use as many '|' characters as you like in your search pattern.

For example, if you wanted to search for three different words such as computer, mouse or keyboard within your document you can use the '|' character to do so as shown: 'computer|mouse|keyboard'. As well, any of those words can be replaced with Regular Expression patterns.

Longest Possible Match

By default, WebDesign will try to match the longest stretch of the pattern it can find. For example, given a document containing this text to be searched;

<B>This text is bold</B>

And a search string of '<.+>', will match the entire string from the first < to the last >. If you only want to match the first <B>, you must turn 'greediness' off.

To turn greediness of you use the '?' character after any of the repetition characters. So to modify the example above, to only get the bold tag, you must search for '<.+?>'

Sub Expressions

You can group your patterns together using paranethesis so that they can be accessed later on when searching and replacing. For example, if you take a certain section of your search string and surround it with '(' and ')' character, you can access the matched string using \1. Thus making it possible to find a certain pattern in your document, and remove everything but a certain portion of the matched string by placing \1 in the replace field.

You can use as many parenthesises as you like and refer to each successive one using \1,\2,...\n where n is the specified sub expression. This is useful both when searching so that a certain portion of the search string must match the found text of a different portion of your search string surrounded by parenthesis. It is, however, more useful when replacing text since you can take a portion of the found text and replace the entire pattern with just a certain section of the found string. Also note that '$&' matches the entire text found by the search pattern. This can be used if you would like to append or prepend text or certain characters to the found text such as appending a copyright symbol to certain matched strings which will not always be the same.

Extension Mechanisms

The following expressions can be used by replacing x with a regular expression pattern. These are more advanced expressions and are not fully explained below.

Pattern Description
(?:x) For grouping without creating backreferences
(?=x) A zero-width positive look-ahead assertion. For example, \w+(?=\t) matches a word followed by a tab with out including the tab in '$&'
(?!x) A zero-width negative look-ahead assertion. For example foo(?!bar) matches any occurrence of foo that isn't followed by 'bar'
(?<=x) A zero-width positive look-behind assertion. For example, (?<=\t)\w+ matches a word that follows a tab without including the tab in $&. Works only for fixed-width-look behind.
(?<!x) A zero-width negative look-behind assertion. For example (?<!bar)foo matches any occurrence of 'foo' that does not follow 'bar. Works only for fixed-width look-behind
[Top]


Copyright 1999-2004 Rage Software. All rights reserved world wide
Back to Index