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:
| Component | Unit | Description |
|---|---|---|
TSynEditSearch | SynEdit.Search | Plain text search, with options for case and whole word. |
TSynEditRegexSearch | SynEdit.RegexSearch | Search with regular expressions. |
TSynEditWildcardSearch | SynEdit.WildcardSearch | Search 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.
Using the search
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:
| Option | Effect |
|---|---|
ssoMatchCase | Case sensitive search. |
ssoWholeWord | Finds only whole words. |
ssoBackwards | Searches backwards. |
ssoEntireScope | Searches the whole text, not only from the caret. |
ssoSelectedOnly | Searches only in the selected text. |
ssoReplace | Replaces the text found. |
ssoReplaceAll | Replaces all the occurrences (with ssoReplace). |
ssoPrompt | Asks for confirmation before each replace (through OnReplaceText). |
Events of the editor
OnSearchNotFound: the text was not found.OnReplaceText: called for each replace whenssoPromptis used; set theActionparameter toraReplace,raSkip,raCancelorraReplaceAll.
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):
| Unit | Dialog |
|---|---|
SynEdit.dlgSearchText | Search text. |
SynEdit.dlgReplaceText | Search and replace text. |
SynEdit.dlgConfirmReplace | Confirmation of a replace. |
Demo
See the Search and Replace demo.
