The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....


Declaration:function SearchAndReplace (var InStream, OutStream: TMemoryStream; SearchExp, ReplaceExp: string; IgnoreCase: boolean; CondToken: integer; Combination: integer; CondText: string; OnUnknownModifier: TSRUnknownModifierEvent): boolean;

The function SearchAndReplace is a powerful search and replace function which replaces any characters of the input stream InStream matching the search expression SearchExp by the replacement expression ReplaceExp. The search expression may be a regular expression. If the parameter IgnoreCaseis TRUE the search is not case sensitive. The replacement expression may be any character string and may contain any number of place holders for the first 9 regular expression tokens of the search expression. The place holders are identified by the character combination @n, with n being a number between 1 and 9 (a double @ character in the replacement string has to be used for inserting @ characters). Replacement substrings addressed by the @-syntax may be modified before reinserting it into the output stream by the following modifiers:

@Ddecrement numeric string by one; ignore if string is non-numeric
@Iincrement numeric string by one; ignore if string is non-numeric
@Bdouble numeric string; ignore if string is non-numeric
@Lconvert to lowercase letters
@Uconvert to uppercase letters
@Vreplace invalid filename characters (all characters which do not comply with the Windows 8.3 filename convention) by underscores
@Yconvert umlauts into ASCII character combinations (see ExpandDiphthongs for details)
@Zconvert to letter and digit-only string; all other characters are replaced by underscores

Modifiers have to precede the replacement token and are only valid for this particular token. You may define custom modifiers simply by inserting an unknown modifier (one that does neither constitute a replacement token nor one of the modifiers listed above) before a replacement token. If an unknown modifier is encountered the callback function OnUnknownModifier is called. The callback function receives both the modifier and the replacement token to be modified. Please note that modifiers must not be specified without a corresponding replacement token. Doing so will result in erroneous results. If you want to use a modifier which does not "modify" anything but adds extra information into the replacement string (for example, a running number) you have to use the replacement token @0 which is always empty.

In addition to the powerful regular expressions the replace process may be controlled by a condition which is defined by the parameter CondToken, Combination, and CondText. The parameter CondToken specifies the regular expression token which is the basis for the condition. There are four possible conditions, specified by the parameter Combination:

0 the regular expression token specified by CondToken is equal to CondText
1 the token is not equal to CondText
2 the token contains CondText
3 the token does not contain CondText

If CondToken is set to a zero value, the evaluation of the condition is switched off.
The function SearchAndReplace returns a TRUE value if the output stream OutStream is different from the input stream InStream. Please note that SearchAndReplace always delivers an output stream. If no parts of the input stream match the search expression and the condition, the output stream is simply a copy of the input stream.

Example: Assuming that the input stream is loaded with the following text:
<title>delphi-webbrowser message #1023</Title>
<link rel=stylesheet type="text/css" href="default_html_styles.css">

Then the statement SearchAndReplace (InStream, OutStream, '<title>.*</title>', '<MyTitle="@U@2">', true, 0, 0, '', nil); creates the following text in the output stream (the second token is '.*' which matches the substring 'delphi-webbrowser message #1023' and is converted to uppercase letters by the modifier @U):

<link rel=stylesheet type="text/css" href="default_html_styles.css">

Last Update: 2012-Oct-20