Although im using c# and the .net lib, im interested in a regex-only solution for some text replacement, and am a bit confused by the final hurdle. (Im using http://gskinner.com/RegExr)
Ive got the string
"Foo {0} Bar {1}"
I can use {[0-9]} to match, but when it comes to replacing, id like to keep the number, i.e. would produce:
"Foo $0$ Bar $1$"
If I had decided I wanted to replace curly braces with dollar symbols (for example).
Replace
\{(\d+)\}with$\1$The parenthesis in the regex “captures” the enclosed portion and it can be accessed in the replacement string using
\1syntax. So if you have multiple capturing groups, the first one is\1and second one is\2etc.Some regex flavors follow
$1instead of\1– in that case you should escape explicit$symbol as$$in the replacement string. Also, the\character itself needs to be escaped as usual in the strings.The curly braces are special characters and hence need to be escaped.