By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Regular expressions: Replacing whole words without replacing non word chararcters

P: n/a
Hi there,
i am trying to use regular expressions to search through a text string
and replace a given whole word.

take the string = "The matsat on the mat!" (bad example i know)

i want to replace the whole word 'mat' with the word 'cat' to give
"The matsat on the cat!"

(matsat was not replaced as only whole word match, cat on the other
hand is replace because non-word characters i.e. !, are allowed)

Given below is the closest i've got using RegExp.replace, with the
following expression
Dim exp As String = "(^|\W+)(mat)($|\W+)"

This correctly identifies whole words (with surrounding non word
charaters) but also replaces the non word characters in the string,
which i wish to preserve.

Can anyone suggest a viable work around or a regular expression that
will match against part of a string without replace the entire of it.
Thanks in advance....

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.