As usual, regular expressions are causing my head to hurt.
I have the following strings (as examples) which I would like to parse:
Client: {Path=ClientName}, Balance: {Path=Balance, StringFormat='{0:0.00}'}
Client: {Path=ClientName}, Balance: {Path=Balance, StringFormat='Your balance is {0:0.00}.'}
I am looking for a regular expression (or any other method) which could split the strings as follows and then get the individual key/value values of each. (The idea is to resolve each one of these to a XAML binding)
String 1: {Path=ClientName}
Path = ClientName
String 2: {Path=Balance, StringFormat='{0:0.00}'}
Path = Balance
StringFormat = {0:0.00}
At the moment I have the following regular expression to split the strings but this gets confused by the value of StringFormat due to the ‘}’ in the value.
(?<!'){(.+?)}(?!')
Any idea how I can achieve this?
Thanks!
It gets really tiring solving this same problem over and over, but here you go:
Technically, you’re doing it wrong, you should use a parser, regular expressions aren’t built to deal with nested matching parenthesis, blah blah blah. We can hack this one together, though, so why not?
The meat of that –
{([^'}]+|'[^']+')}– looks for two things: a) anything that’s not a}or a'character ([^'}]), and b) anything that looks like a string ('[^']+'). It assumes a string is a quote, a bunch of non-quote text, and another quote. Given your examples, this should work.It will, however, fail to match
'This is a string with \'quotes\' in it', because it isn’t designed for escaped quotation marks. Adding this is simple, and involves applying the principles we just applied, so I’ll leave that to you to figure out if you can. You seem to be pretty good with regular expressions, and you at least made a start on this before you asked it, so I think you can figure out how to make it match\'in a string.EDIT: You’re using
's instead of"s. Sorry about that.