4

Part of my app has an area where users enter text into a textBox control. They will be entering both text AND numbers into the textBox. When the user pushes a button, the textBox outputs its text into a string, finds all numbers in the string, multiplies them by 1.14, and spits out the typed text into a pretty little textBlock.

Basically, what I want to do is find all the numbers in a string, multiply them by 1.14, and insert them back into the string.

At first, I thought this may be an easy question: just Bing the title and see what comes up.

But after two pages of now-purple links, I'm starting to think I can't solve this question with my own, very skimpy knowledge of Regex.

However, I did find a hearty collection of helpful links:

Please note: A few of these articles come close to doing what I want to do, by fetching the numbers from the strings, but none of them find all the numbers in the string.

Example: A user enters the following string into the textBox: "Chicken, ice cream, 567, cheese! Also, 140 and 1337."

The program would then spit out this into the textBlock: "Chicken, ice cream, 646.38, cheese! Also, 159.6 and 1524.18."

3
  • 9
    Numbers... or digits? There's a huge difference. Commented Jul 7, 2011 at 20:49
  • Numbers, excluding numbers written as 'one' and 'two' but not numbers written as '1' and '2'. Commented Jul 9, 2011 at 18:59
  • What about -.040? Or 1/3? Or 0x8f? Or 2^8? Or 1e7? Those are all numbers. Commented Jul 9, 2011 at 23:05

2 Answers 2

4

You can use a regular expression that matches the numbers, and use the Regex.Replace method. I'm not sure what you include in the term "numbers", but this will replace all non-negative integers, like for example 42 and 123456:

str = Regex.Replace(
  str,
  @"\d+",
  m => (Double.Parse(m.Groups[0].Value) * 1.14).ToString()
);

If you need some other definition of "numbers", for example scientific notation, you need a more elaboarete regular expression, but the principle is the same.

1
  • Wow! Seems to work wonderfully... but you were missing an ampersand until my edit 1 second in the future. Commented Jul 7, 2011 at 21:11
2

Freely adopted from the sample here

Beware of your regional options (since you're parsing and serializing floating point numbers)

using System;
using System.Text.RegularExpressions;

class MyClass
{
   static void Main(string[] args)
   {
      var input = "a 1.4 b 10";

      Regex r = new Regex(@"[+-]?\d[\d\.]*"); // can be improved

      Console.WriteLine(input);
      Console.WriteLine(r.Replace(input, new MatchEvaluator(ReplaceCC)));
   }

   public static string ReplaceCC(Match m)
   {
       return (Double.Parse(m.Value) * 1.14).ToString();
   }
}

[mono] ~ @ mono ./t.exe
a 1.4 b 10
a 1.596 b 11.4
2
  • Is there any advantage to this over Guffa's answer? Commented Jul 7, 2011 at 21:12
  • The regex is slightly more involved (but not perfect). Otherwise, no (aside from providing more documentation links and being slightly more verbose for clarity, actually compiling and runnign - hehe :))
    – sehe
    Commented Jul 7, 2011 at 21:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.