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
  • Why not, in this case, use Linq to XML? Commented Aug 11, 2010 at 17:00
  • I am not aware of how I can use Linq to Xml. I need to perform a method call on each match. Would this make a difference? Can you provide an example? Commented Aug 11, 2010 at 17:10

1 Answer 1

2
@"\{\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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.