Monday, June 24, 2019

Doing everything in wrong manner



Here is a script to replace all sizes in template project to twice smaller. It is bad idea to use regular expressions for html and it is uncommon to change text on the fly in C# but it works.


public static void Main(string[] args)
        {
            string path = @"email\src";
            Regex sizeRegEx = new Regex("size=\".+\"");
            Regex valueRegEx = new Regex(@"\d+");
          
            foreach(string file in Directory.GetFiles(path, "*.html", SearchOption.AllDirectories))
            {
                string text = File.ReadAllText(file);
                var aStringBuilder = new StringBuilder(text);
              
                foreach(Match m in sizeRegEx.Matches(text))
                {
                    Console.WriteLine(m.Value);
                    Match valueMath = valueRegEx.Match(m.Value);
                    Console.WriteLine(valueMath.Value);
                    aStringBuilder.Remove(m.Index + valueMath.Index, valueMath.Value.Length);
                    int size = int.Parse(valueRegEx.Match(m.Value).Value);
                    string newValue = (size / 2).ToString();
                    aStringBuilder.Insert(m.Index + valueMath.Index, newValue);
                    if(valueMath.Value.Length == 2 && newValue.Length == 1)
                    {
                        aStringBuilder.Insert(m.Index + valueMath.Index + 1, " ");
                    }
                }
                File.WriteAllText(file, aStringBuilder.ToString());
            }
        }
      
        public static string DeleteLines(string input, int linesToSkip)
        {
            int startIndex = 0;
            for (int i = 0; i < linesToSkip; ++i)
                startIndex = input.IndexOf('\n', startIndex) + 1;
            return input.Substring(startIndex);
        }

No comments:

debug magazine archive

  71 jounals still available on issuu with great story of netlabels time.  debug_mag Publisher Publications - Issuu