How to replace [Word] with Word using Regex.Replace and should replace only whole words

I am currently working on a translation project. One of the problems that I have encountered is when I try to replace special character characters.

For example:

[Animal] can be furry. Dog is an [Animal]. 

I need to replace [Animal] with Animal . Please note that I only need to replace the entire word. Thus, the result should be as follows:

 Animal can be furry. Dog is an Animal. 

In addition, as I said, it should be a whole word. Therefore, if I have:

[Animal][Animal][Animal] can be furry. - the result should be

[Animal][Animal][Animal] can be furry. - nothing happened because [Animal] does not match [Animal][Animal][Animal]

Example:

 string originalText1 = "[Animal] can be furry"; string badText ="[Animal]"; string goodText = "Animal"; Regex.Replace(originalText1, Regex.Escape(badText), Regex.Escape(goodText)); 

Everything is good. But, as I said, I need the whole word to be replaced. And with the code above, " [Animal]can be furry " will be replaced with " Animalcan be furry ", which is no no.

so i also tried:

 Regex.Unescape( Regex.Replace( Regex.Escape(originalText1), String.Format(@"\b{0}\b", Regex.Escape(badText)), Regex.Escape(goodText))) 

However, this will not work. And now I'm lost. Please, help.

I would also like to mention that there was the FIRST similar post, but this question did not require replacing the whole word. I scanned the network for almost 3 hours to no avail. Your help will be greatly appreciated. Thanks!

+7
source share
4 answers

I have not tested it, but I would try this:

 Regex.Replace(orginalText, @"\b\[Animal\]\b", "Animal"); 

This will only match [Animal] at word boundaries (\ b)

+1
source

This works for me. Try it and let me know if this is what you are looking for.

 string originalText1 = "[Animal] can be furry"; string badText = @"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))" + Regex.Escape("[Animal]") + @"(?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))"; string goodText = "Animal"; string newString = Regex.Replace(originalText1, badText, goodText); Console.WriteLine(newString); //"Animal can be furry" originalText1 = "[Animal]can be furry"; newString = Regex.Replace(originalText1, badText, goodText); Console.WriteLine(newString); //"[Animal]can be furry" 

Found here .

0
source

I think the easiest way here is to use the look and feel to make sure the text in square brackets is β€œreal”. I'm not sure about your specific requirements, but it looks like you are looking for:

  • Search string enclosed in square brackets (for example, [Animal] )
  • It preceded the beginning of a line, a space, or, possibly, some punctuation.
  • The sequence of the end of a line or a space, or possibly some punctuation (for example, followed by a period in Dog is an [Animal].

The first is easy: \[Animal\] .

For the second, you can use the look to match the previous character:
(?<=(^|\s)) and for the last search: (?=($|\s|\.))

This means that the entire regex will be:

 var pattern = @"(?<=^|\s)\[Animal\](?=$|\s|\.)"; var output = Regex.Replace(input, pattern, "Animal"); 

You may need to add extra punctuation to the wait / lag as needed.

Examples in your question:

 Input: "[Animal] can be furry." Output: "Animal can be furry." Input: "Dog is an [Animal]." Output: "Dog is an Animal." Input: "[Animal][Animal][Animal] can be furry." Output: "[Animal][Animal][Animal] can be furry." Input: "[Animal]can be furry" Output: "[Animal]can be furry" 
0
source

For me this works:

 string s = @"[Animal][Animal][Animal] can be furry. - nothing happened as [Animal] is not the same as [Animal][Animal][Animal] [Animal] can be furry. [Animal] can [Animal] be furry my [Animal] is furry"; string mask = "(^|\\s)\\[Animal\\](\\s|$)"; string rep = "$1Animal$2"; string s2 = ""; s2 = Regex.Replace(mask, rep); /* s2 = "[Animal][Animal][Animal] can be furry. - nothing happened as Animal is not the same as [Animal][Animal][Animal] Animal can be furry. Animal can Animal be furry my Animal is furry" */ 

You can also add "special characters" to the mask:

 string mask = "(^|\\s|'|\")\\[Animal\\](\\s|$|,|\\?|\\.|!|'|\")"; 
0
source

All Articles