匹配字符串中的所有数字,使用正则表达式。

huangapple go评论92阅读模式
英文:

Matching all digits from a string with regex

问题

C#将大值填充到数字E+-num。

使用正则表达式 Regex.Match 可以提取字符串中的所有数字。但是,当前的正则表达式 @"\d+\.*\d*" 会匹配并截断数字,将其舍入到小数部分。要获取完整的数字,可以使用以下正则表达式:

Regex.Match(99999999999995555555555555555555555559999.98.ToString(), @"\d+\.*\d*").Value

这将返回完整的数字 99999999999995555555555555555555555559999.98 而不是舍入的版本。

英文:

I'm trying to extract all numbers from a string.
C# pads large value to number E +- num.

Regex.Match(99999999999995555555555555555555555559999.98.ToString(), @"\d+\.*\d*")

matches only to:

9.99999999999956

clearly rounding part of the number.

How to get all digits, 99999999999995555555555555555555555559999.98

-> 9999999999999555555555555555555555555999998?

答案1

得分: 1

只需这样做:

Regex.Match("99999999999995555555555555555555555559999.98", @"\d+.\d");


[![输入图片说明][1]][1]


  [1]: https://i.stack.imgur.com/GpvC5.png

更新:尝试进行测试

var result1 = 99999999999995555555555555555555555559999.98.ToString();
double result2 = 99999999999995555555555555555555555559999.98;
string result3 = "99999999999995555555555555555555555559999.98";
Console.WriteLine(result1); // 9.99999999999956E+40
Console.WriteLine(result2); // 9.99999999999956E+40
Console.WriteLine(result3); // 99999999999995555555555555555555555559999.98

英文:

Just do this:

Regex.Match("99999999999995555555555555555555555559999.98", @"\d+\.*\d*");

匹配字符串中的所有数字,使用正则表达式。

Update: Try do the test

var    result1 = 99999999999995555555555555555555555559999.98.ToString();
double result2 = 99999999999995555555555555555555555559999.98;
string result3 = "99999999999995555555555555555555555559999.98";
Console.WriteLine(result1); // 9.99999999999956E+40
Console.WriteLine(result2); // 9.99999999999956E+40
Console.WriteLine(result3); // 99999999999995555555555555555555555559999.98

答案2

得分: 0

以下是翻译好的内容:

问题中包含:Regex.Match(99999999999995555555555555555555555559999.98.ToString(), @"\d+\.*\d*")

99999999999995555555555555555555555559999.98 是一个实数文字。这个微软页面 表示:“不带后缀或带有 d 或 D 后缀的文字是 double 类型”。该页面还说明 double 类型的值具有 15 到 17 位十进制数字的精度。

99999999999995555555555555555555555559999.98.ToString() 将该 double 类型的值转换为字符串。

以下的代码有助于展示发生的情况。请注意,numbernumberAsString 都使用 var 声明,以便编译器根据分配的表达式来选择类型。

var number = 99999999999995555555555555555555555559999.98;
Console.WriteLine($"number is {number} and is of type {number.GetType().ToString()}");

var numberAsString = 99999999999995555555555555555555555559999.98.ToString();
Console.WriteLine($"numberAsString is '{numberAsString}' and is of type {numberAsString.GetType().ToString()} and has {numberAsString.Length} characters");

Match match = Regex.Match(99999999999995555555555555555555555559999.98.ToString(), @"\d+\.*\d*");
Console.WriteLine($"match success is {match.Success}");
Console.WriteLine($"match value is '{match.Value}' and has {match.Value.Length} characters");

以上代码的输出是:

number is 9.999999999999555E+40 and is of type System.Double
numberAsString is '9.999999999999555E+40' and is of type System.String and has 21 characters
match success is True
match value is '9.999999999999555' and has 17 characters

匹配值中的一个字符是 .,因此有 16 位小数位,这对应于上述所述的精度。

英文:

The question has: Regex.Match(99999999999995555555555555555555555559999.98.ToString(), @"\d+\.*\d*")

The 99999999999995555555555555555555555559999.98 is a real literal. This Microsoft page states "The literal without suffix or with the d or D suffix is of type double". The page also states that values of type double have a precision of 15 to 17 decimal digits.

The 99999999999995555555555555555555555559999.98.ToString() takes that value of type double and converts it to a string.


The following code helps to show what is happening. Note that number and numberAsString are both declared with var so that the compiler will choose the type based on the assigned expression.

var number = 99999999999995555555555555555555555559999.98;
Console.WriteLine($"number is {number} and is of type {number.GetType().ToString()}");

var numberAsString = 99999999999995555555555555555555555559999.98.ToString();
Console.WriteLine($"numberAsString is '{numberAsString}' and is of type {numberAsString.GetType().ToString()} and has {numberAsString.Length} characters");

Match match = Regex.Match(99999999999995555555555555555555555559999.98.ToString(), @"\d+\.*\d*");
Console.WriteLine($"match success is {match.Success}");
Console.WriteLine($"match value is '{match.Value}' and has {match.Value.Length} characters");

The output from the above is:

number is 9.999999999999555E+40 and is of type System.Double
numberAsString is '9.999999999999555E+40' and is of type System.String and has 21 characters
match success is True
match value is '9.999999999999555' and has 17 characters

One of the characters in the match value is a . thus there are 16 decimal digits which corresponds to the precision stated above.

huangapple
  • 本文由 发表于 2023年3月7日 15:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定