I have a sentence, "Name # 2" ,
I need to replace '#', with "No.",
Final sentence needs to be "Name No. 2".
Code -
string sentence = Regex.Replace("Name # 2", "\\#\\b", "No.");
Apparently the Regex.Replace fails to replace '#' with 'No', is there a correct way to approach the problem, via a regular expression. thanks The reason I am looking for a regex pattern is because there is a generic code which executes which looks something like below
string pattern = string.Format(@"\b{0}\b", context);
sentence = System.Text.RegularExpressions.Regex.Replace(sentence, pattern, suggestion);
context - "#"
sentence - "Name # 2"
suggestion - "No."
Expected sentence - "Name No. 2"
Actual sentence - "Name # 2"
context - "were"
sentence - "He were going"
suggestion - "was"
Expected sentence - "He was going"
Actual sentence - "He was going"
context - "a"
sentence - "He was at a point in time going."
suggestion - "z"
Expected sentence - "He was at z point in time going"
\bmatches a word boundary (between alphanumeric and other). There is no such boundary next to that#in your input.