John Kouraklis 94 Posted February 17, 2019 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
Silver Black 23 Posted February 18, 2019 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
John Kouraklis 94 Posted February 18, 2019 @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
Silver Black 23 Posted February 18, 2019 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
FredS 138 Posted February 18, 2019 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
John Kouraklis 94 Posted February 18, 2019 @FredS What I need is to return 'yes, abc.def' Share this post Link to post
Silver Black 23 Posted February 18, 2019 (edited) 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 February 18, 2019 by Silver Black Share this post Link to post
John Kouraklis 94 Posted February 18, 2019 Yes, but I don't want to write a function from scratch Share this post Link to post
toms 29 Posted February 18, 2019 (edited) Does that function ReplaceWords() work for you? http://codeverge.com/embarcadero.delphi.general/stringreplace-with-a-whole-word-on/1063318 Edited February 18, 2019 by toms 1 Share this post Link to post
FredS 138 Posted February 18, 2019 1 hour ago, John Kouraklis said: What I need is to return 'yes, abc.def' Then you need to figure out what that logic is, it's not word boundaries: https://regex101.com/r/bnXElq/1 1 Share this post Link to post
David Schwartz 430 Posted February 18, 2019 I use the RegExpr library from years back. It's great. Very easy to set up patterns to detect words. Share this post Link to post
John Kouraklis 94 Posted February 18, 2019 Thanks for the RegEx suggestions Share this post Link to post
limelect 48 Posted February 19, 2019 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