【问题标题】:VS2010 Find and Replace numeric listVS2010 查找和替换数字列表
【发布时间】:2016-01-04 22:34:34
【问题描述】:

我需要在 VS2010 中使用正则表达式的帮助。我要找:

Environment.Exit(0); ... 
Environment.Exit(0); 

并将其替换为:

File.WriteAllText("code",1); Environment.Exit(0); ...
File.WriteAllText("code",2); Environment.Exit(0); 

我只需要知道我的程序在哪里终止。所以我在不同的文件中有很多 Environment.exit。

【问题讨论】:

  • 更好的选择是将调用重构为Environment.Exit(..)。从编码的角度来看,这将为您提供单点退出,您可以在离开应用程序之前检查调用堆栈。
  • 我有超过 50 次 Env.Exit() 调用,只是技术上我需要替换这段代码

标签: c# regex search replace


【解决方案1】:

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            string input = "Environment.Exit(0);" +
                           "Environment.Exit(0);";
            string output = Regex.Replace(input, @"Environment.Exit\(0\);", new MatchEvaluator(MyReplacement));
        }   
        static int count = 1;
        static string MyReplacement(Match match)
        {
            return string.Format("File.WriteAllText(\"code\",{0}); Environment.Exit(0);", count++); 
        }
    }
}​

【讨论】:

  • 谢谢,但我仍然希望标准的 Visual Studio 搜索工具能解决我的问题。但正如您所建议的,解决此任务最有效的方法是编写另一个程序来完成主程序的编写。
  • VS 编辑器没有像一些更复杂的编辑器那样增加数值的宏功能。
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 2012-02-13
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
  • 2020-10-13
  • 1970-01-01
相关资源
最近更新 更多