Regular expression for xaml syntax syntax

As usual, regex is making my head ache.

I have the following lines (as examples) that I would like to analyze:

Client: {Path=ClientName}, Balance: {Path=Balance, StringFormat='{0:0.00}'}
Client: {Path=ClientName}, Balance: {Path=Balance, StringFormat='Your balance is {0:0.00}.'}

      

I'm looking for a regex (or any other method) that could split strings like this and then get the individual key / value values ​​for each. (The idea is to allow each of these to be XAML binding)

String 1: {Path=ClientName}

    Path = ClientName

String 2: {Path=Balance, StringFormat='{0:0.00}'}

    Path = Balance
    StringFormat = {0:0.00}

      

I currently have the following regex to split strings, but this is confusing the StringFormat value because of the '}' value in the value.

(?<!'){(.+?)}(?!')

      

Any idea how I can achieve this?

Thank!

0


source to share


1 answer


This is a really tedious solution to this problem over and over again, but here you go:

Technically, you are doing it wrong, you must use a parser, regexes are not built to work with nested matching parentheses, blah blah blah. However, we can hack this together, so why not?

/(?<!'){([^'}]|'[^']+')+}(?!')/

      



The meat of this - {([^'}]+|'[^']+')}

- looks for two things: a) anything that is not a character }

or '

( [^'}]

), and b) anything that looks like a string ( '[^']+'

). It assumes that the string is a quote, a bunch of unquoted text, and another quote. Given your examples, this should work.

However, it will not match 'This is a string with \'quotes\' in it'

as it is not meant for escaped quotes. Adding this is simple and involves applying the principles we just applied, so I'll leave that to you to see if you can. You seem to be very familiar with regexes and you've at least started doing this before you ask for it, so I think you can figure out how to make it match \'

inline.

EDIT: you are using s '

instead "

. Excuse me.

+1


source







All Articles