Find and replace numbers in string with regex

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2012-09-14T21:29:48Z Indexed on 2012/09/14 21:37 UTC
Read the original article Hit count: 205

Filed under:
|

What I'm trying to achieve is to replace the numbers in the string with a new values calculated from the (match * int).

So the string input looks like:

500g Flour
14g Salt
7g Dry yeast
45ml Olive oil
309ml Water

And the result should look like this:

1000g Flour
14g Salt
14g Dry yeast
90ml Olive oil
618 ml Water

row["ingredients"] is a DataRow.

This is where I'm at:

System.Text.RegularExpressions.
        Regex.Replace(row["ingredients"].ToString(), 
                      @"[^/d]", Delegate(Match match) { return match * 2; },
                      RegexOptions.Multiline);

Any solution is greatly appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex