1

Is there any function/procedure as ReplaceString but for whole words on Delphi? I just need to replace substring to the new one when it's a whole word. For example:

substring - > newstring
substring, -> newstring
substring123 -> substring
bummi
  • 27,123
  • 14
  • 62
  • 101
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • 3
    You can read [Is There An Efficient Whole Word Search Function in Delphi?](http://stackoverflow.com/a/1678814/576719), and make your own SearchReplace function for whole words. – LU RD Sep 15 '14 at 17:19
  • I take that back, appearently there is a bug in the SearchBuf() with the `[soWholeWord]` option. See [SearchBuf soWholeWord unexpected output](http://stackoverflow.com/q/25877986/576719). – LU RD Sep 16 '14 at 21:08

1 Answers1

7

You can use the built-in regex library to do this. For example:

{$APPTYPE CONSOLE}

uses
  System.RegularExpressions;

const
  InputString = 'a Substring, substring123, some more text';

begin
  Writeln(TRegEx.Replace(InputString, '\bsubstring\b', 'newstring', [roIgnoreCase]));
end.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490