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

时间:2018-09-25 09:11:34

标签: c# arrays string replace find

我有一个string代表字节数组,它的里面有几组数字(通常为5):编码为0x30..0x390..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[]中-也许有办法吗?

谢谢

3 个答案:

答案 0 :(得分:2)

(从原始问题开始)不清楚您想对数字做什么;让我们提取一个自定义方法来实现它。例如,我实现了 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 来简化代码。 :

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]"

答案 1 :(得分:0)

我不确定,但我想长度会改变,您只想按相反的顺序重新排列数字即可。所以一种可能的方法是:

  • 将字符串放入2个数组中(因此它们是相同的)
  • 遍历其中一个以查找数字区域的开始和结束
  • 在第一个数组中从结束区域到开始区域,然后从开始区域到结束区域写入第二个区域

编辑:未经真正测试,我只是很快就写了:

    string input = "E5-20-36-32-37-20-E0";
    string[] array1 = input.Split('-');
    string[] array2 = input.Split('-');

    int startIndex = -1;
    int endIndex = -1;

    for (int i= 0; i < array1.Length; ++i)
    {
        if (array1[i] == "20")
        {
            if (startIndex < 0)
            {
                startIndex = i + 1;
            }
            else
            {
                endIndex = i - 1;
            }
        }
    }

    int pos1 = startIndex;
    int pos2 = endIndex;
    for (int j=0; j < (endIndex- startIndex + 1); ++j)
    {
        array1[pos1] = array2[pos2];
        pos1++;
        pos2--;
    }

答案 2 :(得分:0)

如果您清楚如何处理数字,则提供解决方案会更容易。

  • 您要随机交换它们吗?
  • 您要撤消订单吗?
  • 您是否想将第二个数字与之前的数字交换?
  • 您想交换...

您可以尝试以下操作(用于反转数字)

string hex = "E5-20-36-32-20-E0"; // this is your input string

// split the numbers by '-' and generate list out of it
List<string> hexNumbers = new List<string>();
hexNumbers.AddRange(hex.Split('-'));

// find start and end of the numbers that should be swapped
int startIndex = hexNumbers.IndexOf("20");
int endIndex = hexNumbers.LastIndexOf("20");

string newHex = "";
// add the part in front of the numbers that should be reversed
for (int i = 0; i <= startIndex; i++) newHex += hexNumbers[i] + "-";
// reverse the numbers
for (int i = endIndex-1; i > startIndex; i--) newHex += hexNumbers[i] + "-";
// add the part behind the numbers that should be reversed
for (int i = endIndex; i < hexNumbers.Count-1; i++) newHex += hexNumbers[i] + "-";
newHex += hexNumbers.Last();

如果开始和结束始终相同,则可以将其简化为4行代码:

string[] hexNumbers = hex.Split('-');
string newHex = "E5-20-";
for (int i = hexNumbers.Count() - 3; i > 1; i--) newHex += hexNumbers[i] + "-";
newHex += "20-E0";

结果:

"E5-20-36-32-20-E0" -> "E5-20-32-36-20-E0"
"E5-20-36-32-37-20-E0"    -> "E5-20-32-37-36-20-E0"
"E5-20-36-12-18-32-20-E0" -> "E5-20-32-18-12-36-20-E0"