I am trying to obtain the value part of this syntax:
[name:value]
I got as far as:
\[name:(\w*)]
Which matches the entire thing. Does anyone know the changes I would need to make to select only value? (Bearing in mind that syntax will also be surrounded by other text.
The
Match.Resultmethod is what you want. See API documentation and documentation on Regex substitution patterns.Use
Regex.Match(input, pattern).Result("$1")to get what you want.To give a complete example, here is the unit test I used to confirm that this solution would work:
The test passes.
You can also go through a string and extract multiple occurrences of the pattern:
Finally, if you need to abstract this to something other than “name”, you can do this: