Matching Start & End Tags

John C. Bland II

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”:http://regexadvice.com/ and “Kodos”:http://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”:http://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.




This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

One Response to “Matching Start & End Tags”

  • John C. Bland II Says:

    Here is a link Paul Schroeder posted on AZGroups in response to my post.

    “http://haacked.com/archive/2004/10/25/UsingRegularExpressionsToMatchHTML.aspx”:http://haacked.com/archive/2004/10/25/UsingRegularExpressionsToMatchHTML.aspx

Leave a Reply

  • Get Adobe Flash playerPlugin by wpburn.com wordpress themes