I have a sentence, "Name # 2",
I need to replace "#" with "No",
The final sentence should be "Name No. 2".
Code -
string sentence = Regex.Replace("Name # 2", "\\#\\b", "No.");
Apparently, Regex.Replace cannot replace '#' with 'No', is there a proper way to approach the problem through regex. thanks The reason I'm looking for a regex pattern is because common code is being executed that looks something like this:
string pattern = string.Format(@"\b{0}\b", context); sentence = System.Text.RegularExpressions.Regex.Replace(sentence, pattern, suggestion);
context - "#"
offer - "Name No. 2"
the sentence is "No."
Expected Proposal - "Name No. 2"
Actual offer - "Name No. 2"
context - were
sentence - "He walked"
the sentence was
Expected Proposal - "He Walked"
Actual sentence - "He walked"
context - "a"
sentence - "He was at a time."
sentence - "z"
Expected Proposal - "He was at a time"
source share