我需要使用正则表达式模式来跟踪浮点数.预先感谢 [英] I need regex patterns for following floating point numbers. thanx in advance

查看:70
本文介绍了我需要使用正则表达式模式来跟踪浮点数.预先感谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

9.8或12.09
//点之前和之后的一位或两位数字

9.8 or 12.09
// one or 2 digits before point and after point

推荐答案

此正则表达式模式很简单

the regex pattern for this would be simple

\d{1,2}.\d{1,2}



似乎您将其保存在字符串中,因此需要在开始和结束处添加\ b,如下所示.

我尝试了下面的代码,它按预期工作了



seems you are holding it in a string, you need to add \b at start and end as updated below.

I have tried following code and it worked as expected

string STR = "10.01,11.11,111.11,1.1,1.11,222.222,22.222";
string[] lines = STR.Split(',');
Regex regex = new Regex(@"\b\d{1,2}\.\d{1,2}\b");
foreach (string line in lines)
{
    Match match = regex.Match(line.Trim());
    if (match.Success)
    {
        Console.WriteLine(line);
    }
}



希望能有所帮助.如果是这样,请将答案标记为解决方案和/或投票.
-Milind



Hope that helps. If it does, mark the answer as solution and/or upvote.
-Milind


您可能想尝试以下方法:
You might want to try this one:
\b(?:\d{1,2}\.+\d{1,2})\b


这篇关于我需要使用正则表达式模式来跟踪浮点数.预先感谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆