简体   繁体   中英

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

I'm working on a translation project right now. One of the issues that I encountered is when I'm trying to replace words special characters.

For example:

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

I need to replace [Animal] with Animal . Please take note that I need to replace the whole word only. So the result should be as followed:

Animal can be furry.
Dog is an Animal.

Also, as I've said, it should be the whole word. So if i have:

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

[Animal][Animal][Animal] can be furry. - nothing happened as [Animal] is not the same as [Animal][Animal][Animal]

Sample:

string originalText1 = "[Animal] can be furry";
string badText ="[Animal]";
string goodText = "Animal";

Regex.Replace(originalText1,  Regex.Escape(badText), Regex.Escape(goodText));

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

so I also tried:

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

Still won't work though. And now I'm lost. Please help.

I'd also like to mention that there's an ALMOST similar post, but that question didn't require the replacement of whole word only. I've looked over the net for almost 3 hours to no avail. Your help will be greatly appreciated. Thanks!

I haven't tested it, but I would try this:

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

That would only match [Animal] at word boundaries (\\b)

This works for me. Try it and let me know if it's what you're 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 .

I think the easiest approach here is to use a look-behind and a look-ahead to make sure the bracketed text is a "real" match. I'm not sure of your exact requirements, but it appears you are looking for:

  1. The search string, enclosed in square brackets (eg [Animal] )
  2. Preceded by the start of the string, or whitespace, or possibly some punctation.
  3. Followed by the end of the string, or whitespace, or possibly some punctuation (eg followed by a period in Dog is an [Animal].

The first one is easy: \\[Animal\\] .

For the second you can use a look-behind to ensure the preceding character is appropriate:
(?<=(^|\\s)) , and for the last a look-ahead: (?=($|\\s|\\.))

Which means the whole regex will be:

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

You may need to add extra punctuation to the look-ahead/behind as appropriate.

For the 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"

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 chars" to the mask:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM