美文网首页
正则表达式自动添加_className

正则表达式自动添加_className

作者: 2b75747cf703 | 来源:发表于2017-07-05 15:20 被阅读66次
image.png
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var files = Directory.GetFiles(@"C:\xxxxx\Client\src", "*", SearchOption.AllDirectories);

            for (var i = 0; i < files.Length;i++)
            {
                var path = files[i].Replace("\\", "/");

                var text = File.ReadAllText(path);

                var newText = Regex.Replace(text, @"([\w.]+)(\s*=\s*[\w.]+.extend\({)([\w\W]+?)(}\);)", (match) =>
                {
                    if (match.Groups[3].Value.Contains("_className"))
                        return match.Value;

                    var _className = "_className: \"" + match.Groups[1].Value + "\"";
                    var _classPath = "_classPath: \"src/" + path.Substring(path.IndexOf("/src/") + 5) + "\"";

                    var content = "\n\t" + _className + ",\n\t" + _classPath + ",\n";

                    return match.Groups[1].Value + match.Groups[2].Value + content + match.Groups[3].Value + match.Groups[4].Value;
                });

                if (newText != text)
                    File.WriteAllText(path, newText);
            }
        }
    }
}

相关文章

网友评论

      本文标题:正则表达式自动添加_className

      本文链接:https://www.haomeiwen.com/subject/omjqhxtx.html