Tips   >   Misc   >   Find And Replace

Find And Replace

The Find and Replace tool in Omnis Studio is a great help to developers. If you aren't already familiar with this tool you should get to know it. This section covers various topics related to the Find and Replace tool.

Use the shortcut key combination Ctrl/Cmnd+F to open the Find and Replace window.

The window defaults to a fairly small size. I recommend stretching the window to a fairly large size and resizing the first column in the log list so that it is about 10 cm (4 inches) wide. Also to be safe, check the Replace all in selected log lines only checkbox. After you do this right-click anywhere on the window and select Save Window Setup.

Tab one lets you enter the find and replace criteria. Tab two lets you select the library and classes, or multiple libraries which you wish to search.

After you enter the find and replace criteria, click the Find All button. The occurrences are listed in the log list.

You can double-click on any line in the log and Omnis Studio will open the appropriate IDE window with the item selected.

You can select any line(s) in the log list and with Replace all in selected log lines only checked click the Replace All button. The find value will be replaced with the replace value for the selected lines if the replacement value is valid.

Regular Expresssions

The Find and Replace dialog window has a check box for Regular Expressions.

Regular expression is a term from the Unix world and is often shortened to regexps.

If you are not experienced with using the regexps (like me) the following tips and examples will be helpful.

Most of us are familiar with using the * and ? wildcard characters when listing files at the DOS prompt or Unix command line.

With regular expresssions the meaning of the * and ? are different and the dot . is added to the mix.

The * character means any number of characters including none of the metacharacter that precedes the * character.

t* will find t, tt, ttt
t* will not find ta, tcp, tub

The . (single dot) character means any single character

ta. will find tab, tan, tab
ta. will not find ta

Combining the . and * together matches any number or any character.

t.* will find t, tt, ttt, ta, tab, tan, tap

If we want to find all the lines of code which use a $search within a $search the following regexps would do the trick:

search.*search

The $ character is a special character used by regexps so you need to escape the $ with a \ backslash character if you want to include the actual $ character in your regexps

\$search.*\$search

The | vertical bar character means or

tt|tan will find tt, ttt, tan, tank

This is only the tip of the iceburg of what you can do with regexps.

Advanced regexps

Square brackets [] give you extra flexibility in your searches.

gr[ae]y will find gray, and grey

li[ce]en[sc]e will find license, licence, lisence, licence

555-[0-9][0-9][0-9][0-9] will find phone numbers beginning with 555-

[Tt][Cc][Pp] will find TCP, tcp, TcP

You can use more than one range.

[0-5a-fA-F] will find any number between 0 and 5 or a letter A to F or a to f

The ^ character means begins with.

Note: If you are searching comment lines remember they begin with the ; character followed by 2 spaces.

^;..555- will find all the comment lines that begin with 555 (The dot . is a single character wildcard)

The $ character means ends with.

$-1212 will find all the lines that end with -1212

For more information on regexps check out
http://www.regular-expressions.info

$findandreplace

Omnis Studio has a $findandreplace class method.

$findandreplace(cFind,cReplace[,bIgnoreCase=kTrue,bWholeWord=kFalse,bRegExp=kFalse])

Note the 4th optional parameter, bRegExp. If you send bRegExp as kTrue Omnis will allow you to use the power of regular expressions (regexps) in your find and replace notation! This makes it possible to do some pretty crazy find and replace code and save yourself a ton of work and time if you need to do some extensive changes to existing code. (Always work with a test copy of your library!)

How about adding regexps intelligence to the correspondence module of an application you have written? With a little creative code you can use OMST's regexps engine for this feature. Here's how.

  1. Create a bogus object class.
  2. Copy the text you want to process to the $desc property of the object class
  3. Send a $findandreplace regexps message to the class.
  4. Copy the text back from the object class $desc property.

Click the Run Demo button in the StudioTips Browser to try it out.

Here's the demo code.

; Correct the spelling of the word 'license' in the following string.

Calculate InputString as 'license, licence, lisense, lisence'

; Copy the string to the $desc property of the object class.
Do $clib.$objects.oRegExps.$desc.$assign(InputString)

; Use regexps for the 'find' parameter
Calculate Find as 'li[cs]en[cs]e'

Calculate Replace as 'license'

; Send a $findandreplace message to the object class.
; $findandreplace(cFind,cReplace[,bIgnoreCase=kTrue,bWholeWord=kFalse,bRegExp=kFalse])
Do $clib.$objects.oRegExps.$findandreplace(Find,Replace,kFalse,kFalse,kTrue)

; Copy back the $desc property of the object class.
Calculate OutputString as $clib.$objects.oRegExps.$desc