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.
inputText.Replace("(\\sWest.\\s)",temp);
It dos not work.How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.
So, yes, regular expressions really only apply to strings. If you want a more complicated FSM, then it's possible to write one, but not using your local regex engine.
String operations will always be faster than regular expression operations.
The \[[^\]]*]\[ matches [ , then any 0+ chars other than ] and then ][ . The (...) forms a capturing group #1, it will remember the value that you will be able to get into the replacement with $1 backreference. [^\]]* matches 0+ chars other than ] and this will be replaced.
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");
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With