ITPub博客

首页 > Linux操作系统 > Linux操作系统 > VS.net 中,使用正则表达式进行查找、替换

VS.net 中,使用正则表达式进行查找、替换

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-09-08 15:54:05 0 删除 编辑
我们就来看一个具体的实例:
比如我们有这样一段代码:
              Assert.AreEqual(null, property._AcquiredDate);
              Assert.AreEqual(null, property._AcreageNumber);
              Assert.AreEqual("San Jose", property._City);
              Assert.AreEqual(null, property._Country);
              Assert.AreEqual("Santa Clara", property._County);
              Assert.AreEqual(null, property._DETAILS);
              Assert.AreEqual("1", property._FinancedNumberOfUnits);
我们想通过替换的方式,把它变成下面这样的代码:
              property._AcquiredDate = null;
              property._AcreageNumber = null;
              property._City = "San Jose";
              property._Country = null;
              property._County = "Santa Clara";
              property._DETAILS = null;
              property._FinancedNumberOfUnits = "1";
这个需求,在写单元测试的时候,会发现经常需要用到。要实现这个功能,我们就可以使用VS.net查询替换功能中的正则表达式.
在使用选项中选择“正则表达式”,然后
查找内容为“Assert\.AreEqual\({[^,]+}, {[^\)]+}\);”;
替换内容为:“\2 = \1;”;
然后全部替换,OK,我们可以省下大量的时间干其他事情了。
如果你想反向替换,也就是把上面的结果替换为最上面的代码,这时候,
查询的内容为:“{[^:b=]*}:b*=:b*{[^;]*};”,
替换内容为:“Assert.AreEqual(\2, \1);”。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-442341/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766206