[SOLVED] Can I use regular expressions with String.Replace in C#?


For example I have code below
string txt=”I have strings like West, and West; and west, and Western.”

I would like to replace the word west or West with some other word. But I would like not to replace West in Western.

  1. Can I use regular expression in string.replace? I used
    inputText.Replace("(\\sWest.\\s)",temp); It dos not work.


No, but you can use the Regex class.

Code to replace the whole word (rather than part of the word):

string s = "Go west Life is peaceful there";
s = Regex.Replace(s, @"\bwest\b", "something");

Answered By – Robert Harvey

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *