zoukankan      html  css  js  c++  java
  • Visual Studio 2017中使用正则修改部分内容

    最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效果如下

    修改前:

    /// <summary>
    /// 条形码
    /// </summary>
     public List<GoodsBarcodeEditModel> Barcodes { get; set; }
    

    修改后:

    /// <summary>
    /// 条形码
    /// </summary>
    [Description("条形码")]
    public List<GoodsBarcodeEditModel> Barcodes { get; set; }
    

    作为一个非处女座,但是有处女座特点的程序猿,牢记着DRY(Don't Repeat Yourself), 不想把时间浪费在不停的Copy-Paste上,于是想着VS的Find and Replace里的正则会不会有支持部分替换的功能,顺着这个想法,找到了微软文档《正则表达式中的替代》,里面的$数值替换捕获组下好满足我的需求。接下来的工作就简单了
    Find and Replace

    (///s<.*
    *)(s*)(///)(s*)(w*)(
    *)(s*///.*)
    
    代码段 正则分组 分组序号
    /// <summary> (///s<.* *) $1
    空格 (s*) $2
    /// (///) $3
    空格 (s*) $4
    条形码 (w*) $5
    换行 ( *) $6
    /// </summary> (s*///.*) $7

    因此使用如下组合,就是得到我们想要的结果

    $1$2$3$4$5$6$7 [Description("$5")]
    

    最终效果如下:
    演示gif

  • 相关阅读:
    左偏树
    output html
    jsp web.xml
    mysql link db
    beamline
    jsp embend java into html / mix scriptlets and HTML
    StringTokenizer
    TreeSet
    centOS 显示中文
    request and response
  • 原文地址:https://www.cnblogs.com/tonqiang/p/9057598.html
Copyright © 2011-2022 走看看