c# decimal Tryparse 来自一个字符串

标签 c# regex

我正在尝试从字符串中获取小数,例如“3.8 lkjhdf”,我怎样才能只获取 3.8。这需要是动态的,因为它可能是“333333.8 kljsdf”

if (!double.TryParse(parsedText, out measurment))
{
    string measurmentString = Regex.Match(parsedText, @"\d+").Value;
    if (double.TryParse(parsedText, out measurment))
    { 
    }
}
paramaterText3.Add(measurment);

最佳答案

您的正则表达式应该是:\d+(\.\d+)? 并且您应该在第二个 double.TryParse 中使用 measurmentString尝试:

if (!double.TryParse(parsedText, out measurment))
{
    string measurmentString = Regex.Match(parsedText, @"\d+(\.\d+)?").Value;
    if (double.TryParse(measurmentString , out measurment))
    { 
    }
}

关于c# decimal Tryparse 来自一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37330865/

相关文章:

c# - .net 键枚举 ToString() 与 BackSlash 一起使用 '\'

c# - SQLite事务保存点问题

c# - 如何从员工的 UserPrincipal 对象获取经理的 UserPrincipal 对象

php - 自动向嵌入式youtube视频添加参数-正则表达式有帮助吗? (PHP,Wordpress)

regex - 当 Perl 中的一个正则表达式中存在两个字符串时替换字符串

c# - 如何检查代码文件的有效性?

c# - 循环遍历 DataRow 中的每个元素

regex - notepad++ - 搜索包含一个字符串但不包含另一个字符串的字符串

javascript - 删除所有字符直到 £ 并删除最后一个字符 ]

regex - 如何使用正则表达式将 Visual Studio 二进制编辑器中的空字节 (0x00) 与查找匹配?