||Regular expressions can help provide the solution. Here is a function that replaces the text matched by a regular expression with whatever you specify. It's syntax is this: |
RegExpReplace(input, pattern, replacement_text). Input is the string you wish to search and replace, pattern is the regular expression that gets matched and replacement_text is what you want to replace it with. Note that you use
<?> to indicate the pattern match.
The first portion of code just sets up some variables and the regular expression:
Function RegExpReplace(strInput, strPattern, strReplace)
Dim regEx, Match, Matches, Position, strReturn
Position = 1
strReturn = ""
Set regEx = New RegExp
regEx.Pattern = strPattern
regEx.IgnoreCase = True
regEx.Global = True
Next the important part. The For ... Next loop below alternates between adding the matched and non-matched portions of the input string.
Set Matches = regEx.Execute(strInput)
For Each Match in Matches
' Below line adds everthing from the current position
' to the next pattern match
strReturn = strReturn & Mid(strInput, Position, Match.FirstIndex+1-Position)
' Then this line adds the match into the replacement string
' and appends it to the result.
strReturn = strReturn & Replace(strReplace, "<?>", Match.Value)
Position = Len(Match.Value) + Match.FirstIndex + 1
' Add any text after the last match
strReturn = strReturn & Mid(strInput, Position, Len(strInput))
RegExpReplace = strReturn
So the following call:
RegExpReplace("Today is a good day.", "\bday\b", "*<?>*") returns: Today is a good *day*.
Or for the example given in the question:
RegExpReplace("Going to work is boring.", "\b[^ ]*ing\b", "<?>") returns: Going to work is boring.