gpt4 book ai didi

C# 如何查找和替换字符串中的特定文本?

转载 作者:行者123 更新时间:2023-11-30 22:59:05 25 4
gpt4 key购买 nike

我有一个代表字节数组字符串,在它里面我有几组数字(通常是5):编码为 0x30..0x39(0..9 数字的代码)。 之前之后每个数字我都有一个空格(0x20 代码)。

示例:

"E5-20-32-36-20-E0"                // "32-36" encodes number "26", notice spaces: "20"
"E5-20-37-20-E9" // "37" encodes number "7"
"E5-20-38-20-E7-E4-20-37-35-20-E9" // two numbers: "8" (from "38") and "75" (from "37-35")

我想找出所有这些组和编码数字中的反转数字:

   8 -> 8
75 -> 57
123 -> 321

期望的结果:

"E5-20-32-36-20-E0"                   -> "E5-20-36-32-20-E0"
"E5-20-37-20-E9" -> "E5-20-37-20-E9"
"E5-20-37-38-39-20-E9" -> "E5-20-39-38-37-20-E9"
"E5-20-38-39-20-E7-E4-20-37-35-20-E9" -> "E5-20-39-38-20-E7-E4-20-35-37-20-E9"

我在 List\String\Byte[] 中有数据 - 那么也许有办法做到这一点?

谢谢,

最佳答案

不清楚(从最初的问题)你想用这些数字做什么;让我们提取一个自定义方法 供您实现它。例如,我实现了reverse:

32          -> 32
32-36 -> 36-32
36-32-37 -> 37-32-36
36-37-38-39 -> 39-38-37-36

代码:

// items: array of digits codes, e.g. {"36", "32", "37"}
//TODO: put desired transformation here
private static IEnumerable<string> Transform(string[] items) {
// Either terse Linq:
// return items.Reverse();

// Or good old for loop:
string[] result = new string[items.Length];

for (int i = 0; i < items.Length; ++i)
result[i] = items[items.Length - i - 1];

return result;
}

现在我们可以使用正则表达式 (Regex) 来提取所有数字序列并用转换后的数字序列替换它们:

  using System.Text.RegularExpressions;

...

string input = "E5-20-36-32-37-20-E0";

string result = Regex
.Replace(input,
@"(?<=20\-)3[0-9](\-3[0-9])*(?=\-20)",
match => string.Join("-", Transform(match.Value.Split('-'))));

Console.Write($"Before: {input}{Environment.NewLine}After: {result}";);

结果:

Before: E5-20-36-32-37-20-E0
After: E5-20-37-32-36-20-E0

编辑:如果reverse 是唯一需要的转换,可以通过删除Transform 并添加Linq< 来简化代码/em>:

using System.Linq;
using System.Text.RegularExpressions;

...

string input = "E5-20-36-32-37-20-E0";

string result = Regex
.Replace(input,
@"(?<=20\-)3[0-9](\-3[0-9])*(?=\-20)",
match => string.Join("-", match.Value.Split('-').Reverse()));

更多测试:

private static string MySolution(string input) {
return Regex
.Replace(input,
@"(?<=20\-)3[0-9](\-3[0-9])*(?=\-20)",
match => string.Join("-", Transform(match.Value.Split('-'))));
}

...

string[] tests = new string[] {
"E5-20-32-36-20-E0",
"E5-20-37-20-E9",
"E5-20-37-38-39-20-E9",
"E5-20-38-39-20-E7-E4-20-37-35-20-E9",
};

string report = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-37} -> {MySolution(test)}"));

Console.Write(report);

结果:

E5-20-32-36-20-E0                     -> E5-20-36-32-20-E0
E5-20-37-20-E9 -> E5-20-37-20-E9
E5-20-37-38-39-20-E9 -> E5-20-39-38-37-20-E9
E5-20-38-39-20-E7-E4-20-37-35-20-E9 -> E5-20-39-38-20-E7-E4-20-35-37-20-E9

编辑 2: 正则表达式解释(详见 https://www.regular-expressions.info/lookaround.html):

   (?<=20\-)         - must appear before the match: "20-" ("-" escaped with "\")
3[0-9](\-3[0-9])* - match itself (what we are replacing in Regex.Replace)
(?=\-20) - must appear after the match "-20" ("-" escaped with "\")

让我们看一下匹配部分3[0-9](\-3[0-9])*:

   3           - just "3"
[0-9] - character (digit) within 0-9 range
(\-3[0-9])* - followed by zero or more - "*" - groups of "-3[0-9]"

关于C# 如何查找和替换字符串中的特定文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52494507/

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