5

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
28g 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.

5
  • 3
    Possibly a silly question, but is there a reason why you don't double the salt along with the other ingredients? Commented Sep 14, 2012 at 21:33
  • does the 500g Flour 14g should be changed to 1000g Flour 28g Salt? if not , i think all the answers must be considered that. Commented Sep 14, 2012 at 21:36
  • Typo, should had been 28g salt Commented Sep 14, 2012 at 21:42
  • 2
    Are you ever expecting fractions? You wouldn't want to "double" 1/4 liter to 2/8 liter. Same for decimal numbers (0.5 tablespoons doubled isn't 0.10 tablespoons)... Commented Sep 14, 2012 at 21:42
  • @James edited to avoid ambiguity, although it rather removes the context of my comment! Commented Sep 14, 2012 at 21:43

4 Answers 4

4

The first problem is that your regex is only matching characters that are not a digit.

Correction: It is using forward slashes instead of a backslash, so it's matching anything that is not a slash or a d

Change your regex to @"\b(\d+)"

Here's a working example on dotnetfiddle.net

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var string1 = "500g Flour 14g Salt 7g Dry yeast 45ml Olive oil 309ml Water";

            var result = Regex.Replace(string1, @"\b(\d+)", Doubler, RegexOptions.Multiline);

            Console.WriteLine(result);

            Console.ReadKey();
        }

        private static string Doubler(Match match)
        {
            return (Convert.ToInt32(match.Value)*2).ToString();
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Not by me, but your regex matches the literal string /w/, followed by one or more ds.
@TimPietzcker, typo, those should have been backslashes, fixed
I still don't get the \w. Why do you want to match A380?
Because I am losing my mind. Was thinking \w was for word boundary, it should have been \b
@TimPietzcker, fixed, except the salt isn't doubling and I have to run, not sure what I missed...
|
1

[^/d] means "any character except slash or the letter d".

To match a number, use \d+.

Comments

0

Hold different type of data into datarow is not good practice. In your sample: "500g Flour" - here you have ingredientValue, measureType, ingredientName. I would recommend create small data class which will represent this row, and all type of data it will be properties of this class.
Benefits are oblivious:
- No needs to replace with regex magic
- Easy to extend and modify

All what you need with regex is to create correct parser of provided data row. But for sure it's more easy then tricks with replace.

Comments

0
void Main()
{
  string ingredients =
@"500g Flour
0.5g Salt
7g Dry yeast
45ml Olive oil
309ml Water";

  string pattern = @"(?:[0-9]+\.?[0-9]*|\.[0-9]+)";

  int multiple = 2;

  Regex.Replace(ingredients, pattern, m => (Convert.ToDecimal(m.Value)*multiple).ToString()).Dump();
}

I did this in LINQPad so don't worry about the Dump() that's just to output the result. And I changed salt to 0.5 just to show that it works with non integers. That pattern is the best I could come up with for matching integers and decimals (with or without any number before the decimal) so it'll match 17, 3.141592, or .5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.