Hello, I am trying to write a regular expression to match and split a custom variable syntax in C#. The idea here is a custom formatting of string values very similar to the .NET String.Format/{0} style of string formatting.
For example the user would define a String format to be evaluated at runtime like so:
D:\Path\{LanguageId}\{PersonId}\
The value 'LanguageId' matches an data object field, and its current value replaces.
Things get tricky when there is a need to pass arguments to the formatting field. For example:
{LanguageId:English|Spanish|French}
This would have the meaning of executing some conditional logic if the value of 'LanguageId' was equal to one of the arguments.
Lastly I would need to support map arguments like this:
{LanguageId:English=>D:\path\english.xml|Spanish=>D:\path\spansih.xml}
Here is an enumeration of all possible values:
Command no argument: do something special
{@Date}
Command single argument:
{@Date:yyyy-mm-dd}
No argument:
{LanguageId}
Single argument-list:
{LanguageId:English}
Multi Argument-list:
{LanguageId:English|Spanish}
Single Argument-map:
{LanguageId:English=>D:\path\english.xml}
Multi Argument-map:
{LanguageId:English=>D:\path\english.xml|Spanish=>D:\path\spansih.xml}
Summary: The syntax can be boiled down to a Key with optional parameter type list or map (not both).
Below is the Regex I have so far which has a few problems, namely it doesnt handle all whitespace correctly, in .NET I dont get the splits I am expecting. For instance in the first example i am returned a single match of '{LanguageId}{PersonId}' instead of two distinct matches. Also i am sure it doesnt handle filesystem path, or delimited, quoted strings. Any help getting me over the hump would be appreciated. Or any recommendations.
private const string RegexMatch = @"
\{ # opening curly brace
[\s]* # whitespace before command
@? # command indicator
(.[^\}\|])+ # string characters represening command or metadata
( # begin grouping of params
: # required param separater
( # begin select list param type
( # begin group of list param type
.+[^\}\|] # string of characters for the list item
(\|.+[^\}\|])* # optional multiple list items with separator
) # end select list param type
| # or select map param type
( # begin group of map param type
.+[^\}\|]=>.+[^\}\|] # string of characters for map key=>value pair
(\|.+[^\}\|]=>.+[^\}\|])* # optional multiple param map items
) # end group map param type
) # end select map param type
) # end grouping of params
? # allow at most 1 param group
\s*
\} # closing curly brace
";