I’m John C Bland II

Husband, Father, Tech Author, Deacon.
Founder ofย Katapult Media, and full-stack polyglot developer.
Political Free Agents Podcast Host.

I create. I launch.

YouTube Channel

I post regular fun on YouTube like me playing the bass and anything else I find fun.ย 

Get Something Built

All project work goes through Katapult Media. Business is open. Let’s chat.

I had a recent problem where I needed to parse a template file and replace the custom tags with different code, etc. Well, we had regex ready to grab a single line tag (ex – <[MyTag]>) then we had it ready to grab a multiline tag (with content between it). The problem came in when I needed to have nested tags. Lemme show a quick example:

bq. <[MyTag]>
some text
maybe some regular html tags
anything you want here
<[SomeOtherTag]>
some content

<[AnotherTag:Singleline]>

Well, the regex we were using would stop at instead of . I tried using lookbacks (or whatever the technical term is) by using \1 but that wasn’t working. Today I was turned on to a few new resources (“RegexAdvice.com”:https://regexadvice.com/ and “Kodos”:https://kodos.sourceforge.net) which helped me shape and mold the regex to a fully functional template parser.

bq. <(\[[a-zA-Z0-9]*\])[^>]*>([\w\W]*?)

I implemented this with C# using the following code:

bq. Regex _contentReg = new Regex(@”<(\[[a-zA-Z0-9]*\])[^>]*>([\w\W]*?)“, RegexOptions.IgnoreCase);
_matchColl = _contentReg.Matches(template);

After this I merely looped over the matches and utilized “Cody’s”:https://blog.xyzpdq.org/ reflection yumminess he had already setup and VOILA! ๐Ÿ™‚

Anyways…all that to say…hopefully this regex helps someone else as it took quite some time to find a solution.