简体   繁体   English

如何使用Regex.Replace用Word替换[Word]并且应该仅替换整个单词

[英]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 . 我需要用Animal替换[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] - 没有什么事发生,因为[Animal][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. 并与上面的代码,“ [Animal]can be furry ”将被替换为“ Animalcan be furry ”,这是一个无无。

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. 我在网上看了将近3个小时都无济于事。 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) 这只会在字边界匹配[Animal](\\ 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] ) 搜索字符串,用方括号括起来(例如[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]. 接下来是字符串的结尾,或者是空格,或者可能是一些标点符号(例如,后跟Dog is an [Animal].一段时间Dog is an [Animal].

The first one is easy: \\[Animal\\] . 第一个很容易: \\[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|\\.)) (?<=(^|\\s)) ,最后一次预测: (?=($|\\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|$|,|\\?|\\.|!|'|\")";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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