5

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"

5
  • 3
    OMG! Why a Regex? Commented Nov 21, 2016 at 12:00
  • @ThomasWeller I have edited my question. Commented Nov 21, 2016 at 12:04
  • The \b matches a word boundary (between alphanumeric and other). There is no such boundary next to that # in your input. Commented Nov 21, 2016 at 12:09
  • 3
    The edit still does not explain why a Regex is needed. Both cases seem to be solvable by a simple string replace. Commented Nov 21, 2016 at 12:11
  • @ThomasWeller I needed to replace a in a sentence like "He was at a point in time going home", I ended up replacing all the 'a's available in the sentence, hence went for regex! Commented Nov 21, 2016 at 12:56

4 Answers 4

6

The current regex won't match if the # is preceded with a word char, a letter, digit or an underscore. That happens because a word boundary meaning is context dependent.

Replace it with any of the fixes below:

string pattern = string.Format(@"(?<!\w){0}(?!\w)", Regex.Escape(context));
                                 ^^^^^^    ^^^^^^

The (?<!\w) and (?!\w) are context-independent and will match a word if not preceded/followed with a word char.

Or, use

string pattern = string.Format(@"(?<!\S){0}(?!\S)", Regex.Escape(context));

to only match a word inside whitespace(s) as (?<!\S) negative lookbehind requires the whitespace or the start of the string before the "word", and the negative lookahead (?!\S) requires the end of string or whitespace after the "word".

Note that Regex.Escape is a must when you deal with user-input/dynamic pattern building and need to use a literal string inside the regex pattern. It will escape all the special chars (like ?, +, etc.) that could ruin the pattern otherwise.

Sign up to request clarification or add additional context in comments.

Comments

4
string test = "Name # 2";

test = test.Replace("#", "No.");

3 Comments

Sorry I needed to go via Regular expression,
test = Regex.Replace(test, "#", "No.");
@Sandepku you should explain (edit your post) why you need to use a regex.
3

You can try:

string sentence = Regex.Replace("Name # 2", "#", "No.");

# does not have to escaped, therefore there is no need for the backslashes.

Comments

2

Using Regex you can do it like so:

Regex.Replace("Name # 2", "#", "No.")

Or just a plain old string.Replace:

"Name # 2".Replace("#", "No.");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.