gpt4 book ai didi

c# - 在 C# 中保持大小写不变的情况下替换文本

转载 作者:太空狗 更新时间:2023-10-29 17:49:41 26 4
gpt4 key购买 nike

我有一组句子需要用来进行替换,例如:

abc => cde
ab df => de
...

我有一个文本在哪里进行更改。但是我无法事先知道上述文字的情况。因此,例如,如果我有:

A bgt abc hyi. Abc Ab df h

我必须替换并获取:

A bgt cde nyi. Cde De h

或尽可能接近,即保持大小写

编辑:因为我看到很多人对此感到困惑,所以我会尝试澄清一下:

我想问的是一种在更换后保留大写字母的方法,但我认为它没有很好地通过(没有很好地解释 thaat 需要什么)所以我将使用真实的文字给出一个更现实的例子。

把它想象成一个 gossary,可以这么说,用它们的同义词替换表达式,所以如果我映射:

didn't achieve success => failled miserably

然后我得到作为输入的句子:

As he didn't achieve success, he was fired

我会得到

As he failled miserably, he was fired

但如果 didn't 被大写,那么 failed,如果 achieve 或 success 被大写,那么 miserable,如果有超过 1 个字母被大写,那么它的 counterpart 也会被大写

我的主要可能性是(我真正想考虑的)

  • 只有第一个单词的第一个字母大写
  • 只有每个单词的第一个字母大写
  • 所有字母大写

如果我能处理这三个我猜已经可以接受的 - 这是更容易的 - 当然,如果可用的话,更深入的解决方案会更好

有什么想法吗?

最佳答案

不确定这会有多好,但这是我想出的:

        string input = "A bgt abc hyi. Abc Ab df h";
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("abc", "cde");
map.Add("ab df", "de");

string temp = input;
foreach (var entry in map)
{
string key = entry.Key;
string value = entry.Value;
temp = Regex.Replace(temp, key, match =>
{
bool isUpper = char.IsUpper(match.Value[0]);

char[] result = value.ToCharArray();
result[0] = isUpper
? char.ToUpper(result[0])
: char.ToLower(result[0]);
return new string(result);
}, RegexOptions.IgnoreCase);
}
label1.Text = temp; // output is A bgt cde hyi. Cde De h

编辑阅读修改后的问题后,这是我修改后的代码(原来是与@Sephallia 的代码类似的步骤..和类似的变量名称 lol )

现在的代码有点复杂..不过我觉得还可以

        string input = 
@"As he didn't achieve success, he was fired.
As he DIDN'T ACHIEVE SUCCESS, he was fired.
As he Didn't Achieve Success, he was fired.
As he Didn't achieve success, he was fired.";
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("didn't achieve success", "failed miserably");


string temp = input;
foreach (var entry in map)
{
string key = entry.Key;
string value = entry.Value;
temp = Regex.Replace(temp, key, match =>
{
bool isFirstUpper, isEachUpper, isAllUpper;

string sentence = match.Value;
char[] sentenceArray = sentence.ToCharArray();

string[] words = sentence.Split(' ');

isFirstUpper = char.IsUpper(sentenceArray[0]);

isEachUpper = words.All(w => char.IsUpper(w[0]) || !char.IsLetter(w[0]));

isAllUpper = sentenceArray.All(c => char.IsUpper(c) || !char.IsLetter(c));

if (isAllUpper)
return value.ToUpper();

if (isEachUpper)
{
// capitalize first of each word... use regex again :P
string capitalized = Regex.Replace(value, @"\b\w", charMatch => charMatch.Value.ToUpper());
return capitalized;
}


char[] result = value.ToCharArray();
result[0] = isFirstUpper
? char.ToUpper(result[0])
: char.ToLower(result[0]);
return new string(result);
}, RegexOptions.IgnoreCase);
}
textBox1.Text = temp;
/* output is :
As he failed miserably, he was fired.
As he FAILED MISERABLY, he was fired.
As he Failed Miserably, he was fired.
As he Failed miserably, he was fired.
*/

关于c# - 在 C# 中保持大小写不变的情况下替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11104750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com