查找字符串中的所有数字

时间:2023-01-24 13:38:16

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.

我的应用程序的一部分有一个区域,用户将文本输入到textBox控件中。他们将在textBox中输入文本和数字。当用户按下一个按钮时,textBox将其文本输出到一个字符串中,查找字符串中的所有数字,将它们乘以1.14,然后将键入的文本吐出到一个非常小的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.

基本上,我想要做的是找到字符串中的所有数字,将它们乘以1.14,然后将它们插回到字符串中。

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

起初,我认为这可能是一个简单的问题:只是Bing的标题,看看会出现什么。

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.

但经过两页现在紫色的链接后,我开始认为我无法用我自己的,非常吝啬的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."

示例:用户在textBox中输入以下字符串:“鸡肉,冰淇淋,567,奶酪!还有,140和1337。”

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

然后该程序将其吐入textBlock:“鸡肉,冰淇淋,646.38,奶酪!还有159.6和1524.18。”

2 个解决方案

#1


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:

您可以使用与数字匹配的正则表达式,并使用Regex.Replace方法。我不确定你在术语“数字”中包含了什么,但这将取代所有非负整数,例如42和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.

如果你需要一些其他的“数字”定义,例如科学记数法,你需要一个更精细的正则表达式,但原理是相同的。

#2


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

#1


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:

您可以使用与数字匹配的正则表达式,并使用Regex.Replace方法。我不确定你在术语“数字”中包含了什么,但这将取代所有非负整数,例如42和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.

如果你需要一些其他的“数字”定义,例如科学记数法,你需要一个更精细的正则表达式,但原理是相同的。

#2


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