fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using System.IO;
  5. public class Test
  6. {
  7. // Demo: https://r...content-available-to-author-only...1.com/r/M9iGUO/2
  8. public static readonly Regex reg = new Regex(@"^h(\d+)\.+\s*(.+)", RegexOptions.Compiled | RegexOptions.Multiline);
  9.  
  10. public static void Main()
  11. {
  12. var inputText = "h1. Topic 1\r\nblah blah blah, because of bla bla bla\r\nh2. PartA\r\nblah blah blah\r\nh3. Part a\r\nblah blah blah\r\nh2. Part B\r\nblah blah blah\r\nh1. Topic 2\r\nand its cuz blah blah\r\nFIN";
  13. var res = ProcessHeadersInText(inputText, 2);
  14. Console.WriteLine(res);
  15. }
  16. public static string ProcessHeadersInText(string inputText, int atLevel = 1)
  17. {
  18. return reg.Replace(inputText, m =>
  19. string.Format("<h{0}>{1}</h{0}>", (int.Parse(m.Groups[1].Value) > 9 ?
  20. 9 : int.Parse(m.Groups[1].Value) + atLevel), m.Groups[2].Value.Trim()));
  21. }
  22. }
Success #stdin #stdout 0.04s 134848KB
stdin
Standard input is empty
stdout
<h3>Topic 1</h3>
blah blah blah, because of bla bla bla
<h4>PartA</h4>
blah blah blah
<h5>Part a</h5>
blah blah blah
<h4>Part B</h4>
blah blah blah
<h3>Topic 2</h3>
and its cuz blah blah
FIN