Tips_todo   >   Misc   >   Find And Replace

$findandreplace

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

You can use the $findandreplace method on a class.

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 Demo button to run the try it out.

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

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.