我正在尝试使用C#在字符串中查找并删除特定模式。

该模式是一个星号,后跟任意数量的数字,后跟.txt

字符串示例:

  • 测试* 123.txt
  • test2 * 1.txt
  • 测试* 1234.txt3
  • test4 * 12.txt123

  • 给定这些示例,所需的结果将是:
  • 测试(“* 123.txt”已删除)
  • test2(“* 1.txt”已删除)
  • test3(“* 1234.txt”已删除)
  • test4123(“* 12.txt”已删除)

  • 如何做到这一点?

    最佳答案

    string pattern = @"\*\d*\.txt";
    Regex rgx = new Regex(pattern)
    input = rgx.Replace(input, "");
    

    关于c# - 使用C#在字符串中查找特定模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5173049/

    10-13 07:24
    查看更多