I have a bunch of short strings in the pattern of:
<text @varible1 more text><, @variable2 text ><@variable3 text text>
the @variableN are place holders, each bracket is a 'section' which indicates that if the enclosed varibale is an empty string, the 'section' will be absent. I'm thinking to use regular expression to extract each section and then re-assemble the whole string based on whether or not the corresponding variable is empty or not. For example, if I pass @variable1='hello' @variabl3='world' the whole string shall return as :
text hello more text, world text
At first I thought maybe I could use enough regex tricks to get the job done. Then I found the 'sections' can possibly nest and also I need to escape a few special characters such as - obviously - '<', '>' and '@'. The more I think about it the more it looks like a DSL to me. So maybe developing a scanner would be a better idea? I know only a little about writing parsers. So I'm kinda stuck, don't know which way to go.
If anybody has experience in this kind of scenario, please shed some light on it. Thanks.
Syntax Examples
<text @varible1 more text><, @variable2 text ><@variable3 text text>
<text @varible1 more text><, @variable2 <, @nestedVaraible> text \<@userName\> > # with nesting and escaping
<text @varible1 more text><, @variable2 text ><@variable3 \@twitterAccount> # escaping‘@
’