tags:

views:

17

answers:

1

I need a .NET Regex that extracts the "field" and "width" values in the following string:

<element
    attribute='{field}'
    attribute='{field,}'
    attribute='{ field }'
    attribute='{ field, 0 }'
    attribute='{field,0}'
    attribute='{ field, 10 }'
    attribute='{field,10}'
    attribute='{ field, 100 }'
    attribute='{field,100}'
/>

I need to capture everything between the { and } characters then extract the field and width. Width is everything between the , and } characters. If no , character then width is Null/Nothing.

+2  A: 
@"\{\s*(?<field>\w+)\s*(,\s*(?<width>\d*)\s*)?\}"

Usage: Use Regex.Matches and extract the groups from each

Edit: You could add the static text in the expression too, but that would make it 'harder' to read IMO.

leppie
Perfect, thank you very much.
AMissico
Tip: you make the wrong assumption that I do not know regex. A correct assumption is I do not know regex enough for this specific case.
AMissico