Skip to content

Search and Replace ​

The search is done by the method SearchReplace of TSynEdit, that uses the search engine assigned to the SearchEngine property. Three engines are available:

ComponentUnitDescription
TSynEditSearchSynEdit.SearchPlain text search, with options for case and whole word.
TSynEditRegexSearchSynEdit.RegexSearchSearch with regular expressions.
TSynEditWildcardSearchSynEdit.WildcardSearchSearch with wildcards: the pattern is converted to a regular expression (it derives from TSynEditRegexSearch).

TSynEditSearch and TSynEditRegexSearch are on the palette; TSynEditWildcardSearch can be created in code. All of them derive from TSynEditSearchCustom, so you can also write your own engine.

pascal
// the engine is chosen by the editor
SynEdit1.SearchEngine := SynEditSearch1;       // or SynEditRegexSearch1

// searches the next occurrence: returns the number of matches found
if SynEdit1.SearchReplace('begin', '', [ssoMatchCase]) = 0 then
  ShowMessage('Text not found');

// replaces all the occurrences in the entire text
SynEdit1.SearchReplace('foo', 'bar', [ssoReplace, ssoReplaceAll, ssoEntireScope]);

Search options ​

The options are a set of TSynSearchOption:

OptionEffect
ssoMatchCaseCase sensitive search.
ssoWholeWordFinds only whole words.
ssoBackwardsSearches backwards.
ssoEntireScopeSearches the whole text, not only from the caret.
ssoSelectedOnlySearches only in the selected text.
ssoReplaceReplaces the text found.
ssoReplaceAllReplaces all the occurrences (with ssoReplace).
ssoPromptAsks for confirmation before each replace (through OnReplaceText).

Events of the editor ​

  • OnSearchNotFound: the text was not found.
  • OnReplaceText: called for each replace when ssoPrompt is used; set the Action parameter to raReplace, raSkip, raCancel or raReplaceAll.

Search engines ​

TSynEditSearch has also run-time methods to scan a text without an editor: FindAll, FindFirst, FindLast, Next, Prev and Replace, with the properties Pattern, Count, CaseSensitive, Whole and Backwards.

Search dialogs ​

The library contains the dialogs used by the demos (and ready to be used in your applications):

UnitDialog
SynEdit.dlgSearchTextSearch text.
SynEdit.dlgReplaceTextSearch and replace text.
SynEdit.dlgConfirmReplaceConfirmation of a replace.

Demo ​

See the Search and Replace demo.

Released under the MPL 1.1 / GPL 2 dual license.