Jump to content
John Kouraklis

How to replace whole words in a string

Recommended Posts

Hi,

I am using TStringHelper.Replace to replace substrings in strings but it seems that it does not respect whole words.

 

Anyone knows how to do this with Replace?

 

Share this post


Link to post
2 hours ago, John Kouraklis said:

Hi,

I am using TStringHelper.Replace to replace substrings in strings but it seems that it does not respect whole words.

 

Anyone knows how to do this with Replace?

 

 

I use StringReplace. Eg.:

Result := StringReplace(strText, strOld, strNew, [rfIgnoreCase, rfReplaceAll]);

It's perfect for me, for years no issues yet.

Share this post


Link to post

@Silver Black

That's what I use now and sadly does not replace whole words.

 

Eg. in this string 'abc and abc.def' if you want to replace 'abc' whole word only the above and the one from stringhelper will replace both instances of 'abc'

 

 

Share this post


Link to post
10 minutes ago, John Kouraklis said:

@Silver Black

That's what I use now and sadly does not replace whole words.

 

Eg. in this string 'abc and abc.def' if you want to replace 'abc' whole word only the above and the one from stringhelper will replace both instances of 'abc'

 

 

 

If you want to distinguish between words, that for definition are separated by spaces, you have to make a proper function that check occourrences of at least a space before and at least a space after the pattern you want to replace as a word. So it will search for the pattern "abc" then check if before or after there are spaces, if so replace it, otherwise it search for the next occourrence.

Share this post


Link to post
19 minutes ago, John Kouraklis said:

'abc and abc.def'

 

The code below will return: 'yes, yes.def'

v := TRegEx.Replace('abc, abc.def', '\babc\b', 'yes', [TRegExOption.roIgnoreCase]);
\babc\b

Options: Case insensitive; Exact spacing; Dot doesn’t match line breaks; ^$ match at line breaks; Numbered capture; Skip zero-length matches

Assert position at a word boundary (position preceded or followed—but not both—by an ASCII letter, digit, or underscore) «\b»
Match the character string “abc” literally (case insensitive) «abc»
Assert position at a word boundary (position preceded or followed—but not both—by an ASCII letter, digit, or underscore) «\b»

Created with RegexBuddy

 

Share this post


Link to post

To identify a word, besides of spaces, you should also check for dots, commas, etc. Eg.: "abc." / "abc," / " abc "

 

@FredS  your regEx expressions does the same of my StringReplace. 😉

Edited by Silver Black

Share this post


Link to post

In RX library use RxStrUtils.pas  and use

 

function ReplaceStr(const S, Srch, Replace: string): string; {$IFDEF RX_D9}inline; {$ENDIF}
{ Returns string with every occurrence of Srch string replaced with
  Replace string. }

 

It dose the job
 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×