logo for MegaDots Duxbury Systems, Inc. logo logo for MegaDots

Chapter 12: Find and Replace

When you are working in a word processor, you often need to find a particular place in your document. MegaDots' Simple find command finds whatever text you ask for. This easy-to-use command is just the beginning of the flexibility and power offered by the find and replace features. You can find and replace something as simple as a specific word or something as complex as the first semicolon in each paragraph of a list. You can save one or more find and replace instructions in a rules file to automatically fine tune your documents. Empowered by these multi-purpose tools for processing text, some MegaDots users have turned into find and replace junkies.

In this chapter we introduce the different ways to find text and substitute replacement text. Since these features give you so many choices, we provide a variety of examples. The examples show how to use find and replace effectively, and you may find some which are very useful in your everyday work. We also list the easy-to-use rules files supplied with the MegaDots program and describe the important tasks they perform.

The Find and Replace Menu

From the MegaDots editor, go to the Tools Menu by pressing F10 T. Press F to select the Find and Replace Menu. In this menu there are seven options along with corresponding shortcut command keys. These are the commands described in this chapter.

Simple Find

With Simple find you can search either forwards or backwards for specific text. To begin a search, press F9 to search forwards, or press Shift-F9 to search backwards. At any point, you can continue your most recent search by pressing Control-N for the Next occurrence or Control-L for the Last occurrence. If your search doesn't find a match, you can search in the other direction with Control-L or Control-N.

There are two different ways of searching for specific text. You may be concerned with matching the letter case exactly in your search. This kind of search is called case sensitive because it pays attention to, or is sensitive to, letter case. Or you may wish to ignore letter case in your search. This kind of search is called case insensitive because it is not sensitive to letter case. When the text you enter in Simple find has at least one uppercase letter, MegaDots assumes that letter case is significant and does a case sensitive search. When you enter text with only lowercase letters, MegaDots assumes that letter case is not important and does a case insensitive search. When you really do need a case sensitive search on search text that contains all lowercase letters, it is easy to do with Complex find, as described later.

Now we'll go through some examples of using the Simple find commands. From the Editor press F3 and open the file C:\Mega24\chicken.meg.

To find the word "chicken" with any letter case, press F9. MegaDots shows you a window (a quick form) called Simple find, with the prompt Enter text:. Type chicken with all lowercase letters as your find string, and press <Enter>. Your cursor will move forward from its current position to the first occurrence of text that matches the find string. It should fall on the first letter of "Chicken" in the title. Because you typed in all lowercase letters, MegaDots did a case insensitive search and matched the letters, whether lowercase or uppercase.

MegaDots has several ways of letting you know what happened in a search. If you have set Audio cues to Messages in the Editor Preferences, then "Found" or "No more matches" will appear on the status line. If you have set Audio cues to Sounds, you hear a pleasant tone or an unpleasant tone.

Now return to the beginning of the file with Control-Home. Press F9 again. Since MegaDots always gives you the option of using your last find string, "CHICKEN" appears again. You can begin typing new text, or edit the text that is there. Press Delete to delete the lowercase c and type in an uppercase C. When you press <Enter> to accept Chicken, the cursor will move past "CHICKEN" in the title to the paragraph starting, "Here's Huck Finn ....". Because you used an uppercase letter in your find string, MegaDots did a case sensitive search.

Now press Control-N. Your cursor will move to the next occurrence of "Chicken." If you keep pressing Control-N, your cursor keeps moving to the next occurrence of "chicken." Then press Control-End to move to the end of the document and then Control-L for the Last occurrence. The cursor moves backwards to the last occurrence of "chicken." If you keep pressing Control-L, the cursor keeps moving backwards to the last occurrence of "chicken." After doing this several times, keep pressing Control-N to move forward again. When your cursor is at the final occurrence of "chicken," the message is "No more matchs." This means that the word "chicken" does not occur after your current location.

Now let's do a backward search. Move to the end of the file with Control-End, and press Shift-F9. This time type "it was" <Enter> in lowercase letters. Your cursor will move to "It was" at the beginning of a sentence. You will soon learn how to search for "it was" with all lowercase letters using Complex find.

If you need to repeat a search that you've previously done, you can use MegaDots' search history shortcut. After pressing F9 or Shift-F9, press F2 at the Enter text: prompt. You will get a list of the last 10 search text strings you have used in MegaDots, with the most recent one at the top. You can move around this list as in any other MegaDots list, either with the cursor keys or with incremental search. When you find the search string that you want, press <Enter> to do that search.

Let's try out the search history list. From the Editor, press Control-Home to return to the top of the current document, then press F9 to do a Simple find. You will see "it was" at the Enter text: prompt, from the last search. Now press F2 to see the search history list, and select "chicken" from the list. Press <Enter> to do the search. Your cursor should fall on the first letter of "Chicken" in the title.

Simple Replace

The MegaDots replace feature lets you change the text that you've found into a replacement string. The Control-G Global replace command does this replacement throughout all or part of your document: from the match found last to the end of the document, or to the block anchor if block marking or hold block marking is on. The Control-Y "Yes, replace this one" command replaces the text you just found and moves the cursor to the next match of the find string.

Let's pretend to be an over anxious editor who does not want any sentence to begin with the word "Because." Press Control-Home to move to the beginning of the document. Now, to find the word "Because" with capital B, press F9, type Because <Enter>. Your cursor will land on the first sentence beginning with "Because." Now press Control-G for Global replace. The Simple replace quick form will prompt Enter text:. Type It's because <Enter>. MegaDots will put the message "5 replacements" on the status line. "Because" with capital B occurred five times in this document, and both occurrences have been changed to "It's because."

Now return to the beginning of the file with Control-Home. We want to change some occurrences of "chicken" into "fine chicken." Press F9 again, and type chicken <Enter>. To move forward several occurrences, press Control-N several times. When you come to an occurrence you want to change, press Control-Y. The Simple replace quick form prompts Enter text:. When you type fine chicken <Enter>, MegaDots makes the change and moves the cursor to the next "chicken." Press Control-N to move to the next "chicken" without any changes. Keep pressing Control-Y and Control-N as much as you like in order to get used to using Simple replace.

Now, press Control-End to move to the end of the document and then press Control-Y. Since you are at the end of the document, you might expect MegaDots to say "no more matches." Instead it changes the last "warn't" found and moves to the next one. Similarly, press Control-End and Control-G. MegaDots moves the cursor to the last "chicken" found and changes "chicken" from there to the end of the file.

Since Control-Y and Control-G use the location of the last match rather than the current cursor position, you can move around freely as you edit, without losing your place for making replacements. If you have moved to another part of the document and want to make a replacement after the cursor position, just press Control-N first to move to the next match after the cursor, and then press Control-Y or Control-G to replace.

Sometimes when you use Control-G or Control-Y MegaDots won't be sure if you want to continue with the previous replacement. If so MegaDots will present the Simple find quick form first and then the Simple replace quick form.

As a shortcut, you can specify a global replacement string along with the find string. To do this, use a double backslash (\\) to separate the find string from the replace string. For example, you can type F9 hello\\goodbye <Enter> to immediately change every occurrence of hello with goodbye.

Find and Replace with a Marked Block

When you have a block marked, the find and replace commands behave differently. When you begin marking a block with Control-X and issue a find command, the cursor will move to the next occurrence of your search string, and the block will follow. However, when you begin holding a block with Shift-Control-X and issue a find command, MegaDots will search only the text inside the block. When you use global replace with block marking or holding on, MegaDots does the replacements between the block anchor and the last item found.

Using MegaDots Rules Files

A MegaDots rules file is a special MegaDots file which automatically instructs MegaDots to perform a set of find and replace operations specified in the file. Using rules files can greatly automate many text processing operations. When you invoke a rules file, it acts on your entire document, or on your marked block if there is one.

Rules files always have the file extension .MDR. To invoke a Rules File, press Alt-F9 and type the name of the file without the .MDR extension. MegaDots looks for the rules file in your MegaDots program directory or your current directory.

We have supplied a number of very useful rules files in your MegaDots program directory. They include:

There may also be some others. Check for files with the .MDR extension in your MegaDots program directory.

As an example, suppose you accidentally had the caps lock on. To change uppercase letters to lowercase in the affected text, mark the text as a block, press Alt-F9, and answer the Enter name: prompt with LOWER. That's all there is to it. If you'd like to create your own rules file, first read the next section on complex find and replace, then open a new MegaDots file, and enter your complex find/replace strings, one per line. Then save your rules file with a .MDR extension. Now press Alt-F9, and enter the name of your rules file. Voila! Feel free to look at any of the included rules files with the MegaDots editor. This can help you to learn how to write your own rules files.

Using Complex Find and Replace

Complex find and replace is a very powerful tool for processing text. The Complex find and replace commands let you accomplish some tasks which you cannot do as efficiently or at all with the Simple commands. We use the word "complex" because these commands can accomplish complex tasks, not because they are hard to use.

Here are some important uses of Complex find and replace:

Using Complex find and replace is very similar to using simple find and replace. To begin a Complex find, you press Control-F9 to search forward or Control-Shift-F9 to search backward. Control-N, Control-L, Control-G, and Control-Y work as in Simple find and replace.

The only difference is in how you specify the text to find and the replacement text. In complex find and replace, you use a number of special characters to write expressions which can be as complex as small computer programs.

When you press Control-F9 or Control-Shift-F9, the Complex find quick form prompts you for a find string. For the find string in Simple find, you just type the characters you want to find. In Complex find, however, many letters and symbols stand for special things. So, to search for specific text exactly in Complex find, you must enclose the text between quotation marks. If you leave out a closing quotation mark at the end of your find string, MegaDots assumes it for you. If you enclose text between single quote marks instead of quotation marks, MegaDots will do a case insensitive search on the text. As with quotation marks, you can leave out a closing single quote mark at the end of your find string and MegaDots will assume it for you.

As described above, using the Simple find command with all the letters lowercase gives a case insensitive search, ignoring letter case. To find the text with only lowercase letters, use Complex find. At the Enter text: prompt in the Complex find quick form, type a quotation mark followed by the exact text. If the text includes a quotation mark, use two quotation marks in its place. If you use Complex replace, fill in the Complex replace quick form with a quotation mark followed by the replacement text. Again, if a quotation mark is part of the replacement text, use two quotation marks in its place.

Here is an example of replacing "he says" with "he said" in huck.meg. Press Control-Home and Control-F9. Enter the text "he says, including the opening quotation mark. Then press Control-G. Enter the text "he said, including the opening quotation mark. MegaDots reports 4 replacements. If you had done this global replace with Simple find and replace, MegaDots would have done 7 replacements, with three of them putting lowercase h at the beginning of a sentence.

This example is not terribly exciting. The power of Complex find and replace lies in many other uses.

Complex Find

You can build many factors, simple building blocks, into a Complex find string. Then MegaDots searches for text matching the first factor followed by text matching the second factor and so on. Before giving the list of all the different kinds of factors, here are a few examples.

If you do Simple find for the letters "he" in huck.meg, you find them embedded in many other words. To find just the word "he," press Control-F9. Give the find string k'he'k. If you keep pressing Control-N, you find only "he" as a word, with uppercase or lowercase h. In this find string, k stands for any white space or literary punctuation character, and enclosing he in single quotes does a case insensitive search on he.

Now let's move on to a more sophisticated example. Copy the file intguide.meg (the MegaDots Interface Guide) from the MegaDots directory into your working directory, and load it into MegaDots. Suppose that you wish to find phone numbers in the file. Press Control-F9 to begin a complex find. Enter the find string ~(d/3~)bd/3~-d/4q, then press enter. This takes you to the phone number for Enabling Technology under Bookmaker. Pressing Control-N takes you to the phone number for Blazie Engineering under Braille Blazer. If you keep pressing Control-N or Control-L, you will find various phone numbers. Please don't send us the phone bill. In this find string, the ~( stands for an open parenthesis, the d/3 stands for 3 digits, the ~) stands for a close parenthesis, b stands for a space, d/3 stands for 3 digits again, ~- stands for a hyphen, d/4 stands for 4 digits, and q stands for a blank or punctuation. Then return to the beginning of the file and do complex find for wd/3~-d/3~-d/4q. This time you find phone numbers, with the area code written differently. Finally, you can search for both forms of phone number together with the find string O~(d/3(~)b|~-)d/3~-d/4q. Pressing Control-N repeatedly shows that we weren't very consistent.

Now it's time to learn what these enticing codes mean and how to use them. Here is the list of the different kinds of factors that you can build into a Complex find string. In this list, we use C1 and C2 to stand for any character; N1, and N2 to stand for any number; F1 and F2 to stand for any factors; and S1 and S2 to stand for any complex find strings.

Specific Characters

Special Characters

Classes of Letters and Digits

Classes of Blank and Punctuation Characters


Factors Using Modifiers

When a complex find operation does find a string in your text, it puts your cursor at the beginning of the string, unless you used braces in the find string. If you used braces, it puts your cursor at the beginning of the string in your text which matches the part of the find string after the left brace.

Some Sample Find Strings

Testing Variables

You can also test variables in a find string. There are five built-in variables you can test: style, group (the style group), level (the level in a hierarchical style), trans (the type of translation), and emph (the kind of emphasis). When you test a variable, enclose the name inside of brackets. The bracketed name is followed by a comparison operator followed by either a factor or a number value.

The comparison operators are:

Here are some examples of using variables:

Complex Replace

As with Simple find, after doing a complex find you can press Control-Y for one replacement or Control-G for doing a replacement globally throughout all or part of the document. MegaDots prompts you for the complex replace string. To exactly specify characters in the replace string, enclose them in double quotes. As in the complex find string, there are also some special codes for use in the replace string which are not enclosed in quotes:

Specific Characters

To set variables, use the variable name inside of brackets followed by a colon, and then specify the value. The value you specify may be a string inside of double quotes or a number. For example, the replace string [style]:"Body text" sets the style to Body text. The replace string [level]:2 sets the hierarchy level to 2.

When you are performing either simple find (F9) or complex find (Control-F9), you can specify a replace string right there for global replace. To do this, use a double backslash (\\) to separate the find string from the replace string. For example, you can type Control-F9 'david'\\"David" to immediately perform this global replace.

Some Examples of Complex Replace

1. Suppose you have optically scanned a glossary, and you want to use the italicized entries as guide text (guide words on the bottom line). Press F2 and create a sample document called complex.meg. Type a few paragraphs containing a word, phrase, or name in italics followed by its definition. If your display is WYSIWYG, press Alt-W to switch to Format markup. Here is how to change the italics markup to guide text markup. Press Control-F9 and give the find string #["Ei"]#. The #[ is for arrow beginning a markup command. The O~\ is for an optional backslash. The "Ei" is for uppercase E, lowercase i. The ]# is for the arrow ending a markup command. Finally, the braces mean that only the letters E, i will be deleted. When you press <Enter>, your cursor moves to the start italics markup. When you press Control-N, it moves to the end italics markup. Press Control-L to return to the first occurrence, and press Control-G. For the replace string, enter "Eg". If you have typed three paragraphs, you get the message 6 replacements. You see that the Ei start italics markup has changed to the Eg start guide text markup and the \Ei end italics markup has changed to the \Eg end guide text markup.

2. Sometimes documents brought into MegaDots from optical scanning systems have extra paragraph breaks. (Note: If you really have this problem with a file imported as ASCII Document, first try importing it again as ASCII Quick.) Usually a paragraph break followed by a lowercase letter should really be a space. Type some more text which is broken up by unwanted paragraph breaks. Move to the beginning of this text. Press Control-F9 and enter the find string {Eg}s. The Eg stands for end of paragraph, the braces around it mean that only the end of paragraph will be deleted, and the s stands for any lowercase letter. Your cursor lands on the first end of paragraph followed by a lowercase letter. Now press Control-G. For the replace string enter " ". Any end of paragraph followed by a lowercase letter changes to a space. The braces prevent the lowercase letter from being deleted in this replacement.

3. You have an imported document with several different levels of headings. You want the first heading marked as Heading level 1 and the others marked as Heading level 2. The find string is Eg{}[style].='hea'. The replace string is [style]:"Heading level 2".

4. To change all uppercase letters to lowercase, the find string is u and the replace string is s. See the section below about the rules files LOWER.MDR and UPPER.MDR.

5. Now suppose you have an address list, where each paragraph contains all the items for one person, with semicolons between the items. You want to change the first semicolon in each paragraph to a colon. The find string is Eg*{~;}, and the replace string is ~:. If there is no end of paragraph before the first paragraph, you need to change the first one by hand.

Writing MegaDots Rules Files

We have supplied several rules files as part of the MegaDots package. But you can write your own rules files to automate your own common text processing tasks.

A rules file contains a list of instructions, or rules, each specifying a find and replace operation. When you invoke the rules file with Alt-F9, MegaDots performs all of these find and replace operations on your entire document, or on the marked block if there is one. Even if a rules file contains only one rule, saving it in a file means you don't have to come up with the find and replace strings each time you need them.

You already know almost everything you need to write a rules file. You can create a rules file in whatever file format you want, but we recommend writing them as MegaDots files. Put any rules file in your MegaDots program directory or in the current directory when you use it. Put each replacement rule in a new paragraph. Each rule, written as a Complex replace operation, contains the Complex find string, then two backslashes, and then the Complex replace string. In particular, to exactly specify the characters to find or to insert, you must enclose them between quotation marks. You can also include comment paragraphs in a rules file. Just begin a paragraph with two percent signs to make it a comment paragraph. The rules files that come with MegaDots have comment lines explaining how to use them.

The one remaining kind of paragraph in a rules file contains just two backslashes. It changes the way that MegaDots works through your document as it makes the specified changes. Ordinarily, MegaDots "stays on" the first character of your document and applies all the rules listed, then moves to the second character of the document and applies all the rules, and so on until it gets to the last character in the document. But sometimes we want MegaDots to apply one group of rules on the whole document and then go back to the beginning of the document to apply the next group of rules. A paragraph with just two backslashes ends one group of rules and starts the next group.

Examining the rules files supplied with MegaDots is a good way to learn how to write your own rules files. First let's examine the rules file CHARS.MDR. It counts the characters in your document but does not make any changes. The only rule is ?{}\\ with nothing after the two backslashes. ? stands for any character. The empty braces say to insert the replacement text after the character without deleting anything. Since the replacement text is nothing at all, no change is made. Since this happens for every character in your file, the reported number of replacements is the number of characters in your file. Use this rules file with WYSIWYG on, so that it does not count the characters in "section info" paragraphs, like those forcing a new page.

The rules file WORDS.MDR counts the words in your document in a similar way.

The rules file LOWER.MDR changes all uppercase letters to lowercase, including accented letters. This is especially useful if you had the caps lock on unintentionally. Mark the affected text as a block, press Alt-F9, and specify the rules file lower. Let's examine this file. The first rule u\\s changes unaccented uppercase letters to lowercase. u finds any unaccented uppercase letter. When a replacement is made, the uppercase letter is deleted. But the s in the replace string puts in a lowercase copy of the deleted text, in this case the lowercase letter.

The second rule is X(#128-#206u)\\s. The first rule does not find accented letters, represented internally as a special character followed by the underlying letter. You can see the internal representation by switching Markup view to All markup (press Control-Z M A). The letter in this representation is "invisible," so that you do not find an accented e when you are looking for a regular e. The X in the second rule tells MegaDots to look at all the text, no matter what. The X applies to everything inside the parentheses, because the parentheses make that a factor. The #128-#206 covers all the possible special characters used to introduce accented letters. The find string finds one of these special characters followed by a letter. When this is found, it is deleted. But the replacement string s inserts a lowercase copy of the deleted text.

The rules file UPPER.MDR is similar, changing all lowercase letters to uppercase.

OUTLINE.MDR works on an outline where everything is marked as hierarchy level 1. It changes the levels, based on the nature of the label for that entry.

MENU.MDR works on scanned menus. It does some cleanup, inserts guide dots before prices, and assigns styles appropriate in a menu.

If you want clarification on these rules files or any others supplied with MegaDots, read the comment lines, which start with two percent signs.