Search This Blog

Friday, April 27, 2012

Visual Studio 2010 regexp replace decimal delimiter

I had to replace decimal delimiter using Visual Studio 2010’s regular expression built-in mechanism – i.e. “ctrl+h” or ‘find and replace’ option.

The idea was to replace tags in XML:
<value>17,1</value>

with:
<value>17.1</value>

I ended up using “tagged expressions” or “back references” in regexp which IS supported by VS built-in regex. To enter a back reference you simply use \1 (for the first back reference) and tag references by surrounding them with {} braces.

Find what:
\<value\>{-*}{[0-9]+},{[0-9]+}\</value\>

Replace with:
\<value\>\1\2.\3\</value\>

Possible minus sign is the first back reference, digits before coma are the second reference and digits after coma are the third reference.

No comments:

Post a Comment

If you like this post, please leave a comment :)