7

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!

4

4 回答 4

1

我还没有测试过,但我会试试这个:

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

那只会在单词边界处匹配 [Animal] (\b)

于 2012-10-15T11:58:20.977 回答
0

对我来说,这有效:

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" */

您还可以在掩码中添加“特殊字符”:

string mask = "(^|\\s|'|\")\\[Animal\\](\\s|$|,|\\?|\\.|!|'|\")";
于 2012-10-15T12:28:34.640 回答
0

这对我有用。试试看,让我知道它是否是您正在寻找的东西。

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"

在这里找到。

于 2012-10-15T12:14:38.763 回答
0

我认为这里最简单的方法是使用后视和前瞻来确保括号内的文本是“真正的”匹配。我不确定您的确切要求,但您似乎正在寻找:

  1. 搜索字符串,括在方括号中(例如[Animal]
  2. 前面是字符串的开头,或者空格,或者可能是一些标点符号。
  3. 后跟字符串的结尾,或空格,或者可能是一些标点符号(例如,后跟一个句号)Dog is an [Animal].

第一个很简单:\[Animal\]

对于第二个,您可以使用look-behind 来确保前面的字符是适当的:
(?<=(^|\s)),最后一个是look-ahead:(?=($|\s|\.))

这意味着整个正则表达式将是:

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

您可能需要根据需要在前瞻/后视中添加额外的标点符号。

对于您问题中的示例:

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"
于 2012-10-15T12:15:57.673 回答