Search the Community
Showing results for tags 'string replace whole word'.
-
StringReplace is great but sometimes you want to replace a whole instead of partially, for example StringReplace will replace the word 'word' in 'wording', whereas this function won't do that. Function: ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringReplaceWholeWord ; Description ...: Replaces substrings in a string, but will replace the whole word instead of if the word is contained within another. ; Syntax ........: _StringReplaceWholeWord($sString, $sSearchString, $sReplaceString[, $fCaseSensitive = Default]) ; Parameters ....: $sString - The string to evaluate. ; $sSearchString - The substring to search for. ; $sReplaceString - The replacement string. ; $fCaseSensitive - [optional] Use case-sensitivity. Default is False. ; Return values .: Returns the new string, the number of replacements performed is stored in @extended. ; Author ........: guinness ; Remarks .......: Additional help and advice from SmOke_N. ; Example .......: Yes ; =============================================================================================================================== Func _StringReplaceWholeWord($sString, $sSearchString, $sReplaceString, $fCaseSensitive = Default) Return StringRegExpReplace($sString, ($fCaseSensitive ? '(?-i)' : '(?i)') & '(?<!-)\b' & $sSearchString & '\b(?!-)', StringReplace($sReplaceString, '\', '\\')) EndFunc ;==>_StringReplaceWholeWordExample use of Function: ConsoleWrite(_StringReplaceWholeWord('This is a simple string with a Word and worded.', 'word', '(non-word)', Default) & @CRLF) ConsoleWrite(_StringReplaceWholeWord('This is a simple string with a Word and worded.', 'Word', '(non-word)', True) & @CRLF) ConsoleWrite(_StringReplaceWholeWord('This is a simple string with a Word and worded.', 'word', '(non-word)', True) & @CRLF)SmOke_N's version: