Working with large and complex regular expressions is very difficult. But now I discovered a method which makes everything easier - so this is Lessons Learned #1 from the development of 2sxc 7.
While we were working on 2sxc 7 we needed to improve the token engine. We had used the standard DNN engine so far but it had a few problems like:
- It removed empty [] from our templates - which messes um some very common AngularJS scripts
- We needed support for tokens-in-tokens - like [QueryString:SortDirection||[App:Settings:DefaultSort]] to allow queries to do things like "Try to get the sort-direction from the URL, but if it's not there, use the default value configured in the App"
The original RegEx - impossible to work with
So I had to figure out how to add this - based on the Regular Expression found within DNN. Here it is - can you work it out?
(?:\\[(?:(?<object>[^\\]\\[:]+):(?<property>[^\\]\\[\\|]+))(?:\\|(?:(?<format>[^\\]\\[]+)\\|(?<ifEmpty>[^\\]\\[]+))|\\|(?:(?<format>[^\\|\\]\\[]+)))?\\])|(?:(?<object>\\[)(?<property>[A-Z0-9._]+)(?:\\|(?:(?<format>[^\\]\\[]+)\\|(?<ifEmpty>[^\\]\\[]+))|\\|(?:(?<format>[^\\|\\]\\[]+)))?\\])|(?<text>\\[[^\\]\\[]+\\])|(?<text>[^\\]\\[]+)
Nobody I know can read this.
The best Tool I know to work with RegEx: Expresso
Download it here.
Now the tool won't help too much, but a bit.
Commenting your Regular Expressions
While fiddling around with the expression, I found out that I could add comments to my RegEx. This surprised me, as I had never seen this before. Turns out you must enable a feature, and you're ready to go. Make sure you also ignore white-space so that you can indent your expressions. In Expresso the feature is here:

You can then begin writing multi-line Regular Expressions with #-hashes in front of your comments, like this:
# start by defining a group, but don't give it an own capture-name
(?:
# Every token must start with a square bracket
\[(?:
# then get the object name, at least 1 char before a :, then followed by a :
(?<object>[^\]\[:\s]+):
# next get property key - can actually be very complex and include sub-properties; but it ends with a [,| or ]
# note that after the first character it can contain : because sub-properties must be in this key
(?<property>[^\]\[\|\s\:]+[^\]\[\|\s]*))
...
This will make your life much easier. It's still hard - as regular expressions are - but it becomes workable.
Using Commented RegEx in C#-Code
This too is not too hard - just paste the RegEx into a multi-line variable in C# using the @"..." syntax because it allows you to write across multiple lines. Then enable White-Space and Multi-Line in your RegEx object - like this:

Hope this helps you!
With love from Switzerland,
Daniel