Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

  • Thread starter Thread starter Avi
  • Start date Start date
A

Avi

Hi all,

I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

Thanks,
Avi
 
Avi said:
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

In a regular expression pattern you can use \b to indicate a word
boundary so that this code

string example = "foo foobar barfoo foo";
string result = Regex.Replace(example, @"\bfoo\b", "baz");
Console.WriteLine(result);

outputs

baz foobar barfoo baz
 
Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write(myNewText)
 
ThatsIT.net.au said:
Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write(myNewText)

That doesn't make it any better.

Given this replacement:

Dim oldWord = "all"
Dim newWord = "none"

you would get the string "once upon a time in a smnone town "

Also notice the extra space added at the end...
 
Avi said:
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

Thanks,
Avi

As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(oldValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(text, pattern, newValue);
 
As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(oldValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(text, pattern, newValue);



Thanks for the help.

string example = "foo foobar barfoo foo";
string result = Regex.Replace(example, @"\bfoo\b", "baz");
Console.WriteLine(result);
Works out nicely.


-Cnu
 
Duggi said:
Thanks for the help.

string example = "foo foobar barfoo foo";
string result = Regex.Replace(example, @"\bfoo\b", "baz");
Console.WriteLine(result);
Works out nicely.


-Cnu

Yes, that works if you have full control over the input, so that you can
be sure that it doesn't contain any characters that have a special
meaning in a regular expression.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top