Can anyone help me with this one?
My objective here is to grab some info from a text file, present the user with it and ask for values to replace that info so to generate a new output. So I thought of using regular expressions.
My variables would be of the format: {@<num>[|<value>]}.
Here are some examples:
{@1}<br>
{@2|label}<br>
{@3|label|help}<br>
{@4|label|help|something else}<br><br>
So after some research and experimenting, I came up with this expression: \{\@(\d{1,})(?:\|{1}(.+))*\}
which works pretty well on most of the ocasions, except when on something like this:
{@1} some text {@2|label} some more text {@3|label|help}
In this case variables 2 & 3 are matched on a single occurrence rather than on 2 separate matches…
I’ve already tried to use lookahead commands for the trailing } of the expression, but I didn’t manage to get it.
I’m targeting this expression for using into C#, should that further help anyone…
A regular expression which can be used would be something like
This will prevent reading over any closing
}.Another possible solution (with slightly different behaviour) would be to use a non-greedy matcher
(.+?)instead of the greedy version(.+).Note: I also removed the
{1}and replaced{1,}with+which are equivalent in your case.