Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Pattern for string replace

Tags:

c#

regex

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"

like image 600
Sandepku Avatar asked Nov 21 '16 11:11

Sandepku


People also ask

How do I replace a regular expression with a string?

The Regex.Replace(String, String, MatchEvaluator) method is useful for replacing a regular expression match if any of the following conditions is true: The replacement string cannot readily be specified by a regular expression replacement pattern. The replacement string results from some processing done on the matched string.

How do I replace a string with a pattern?

String.prototype.replace () The replace () method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.

How do I match strings with regex?

With RegEx, you can match strings at points that match specific characters (for example, JavaScript) or patterns (for example, NumberStringSymbol - 3a&). The .replace method is used on strings in JavaScript to replace parts of string with characters.

What are substitution patterns in regular expressions?

Substitutions in Regular Expressions. Substitutions are language elements that are recognized only within replacement patterns. They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string.


2 Answers

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.

like image 137
Wiktor Stribiżew Avatar answered Sep 25 '22 13:09

Wiktor Stribiżew


string test = "Name # 2";

test = test.Replace("#", "No.");
like image 22
mybirthname Avatar answered Sep 25 '22 13:09

mybirthname