C# 如何使用正则表达式从字符串中获取数值

C# 如何使用正则表达式从字符串中获取数值,c#,regex,C#,Regex,如何使用正则表达式从以下字符串中获取数值 AD .6547 BNM 结果应该是 .6547 应该这样做和一些c#代码: 试一试 它将选择“.”及其后面的所有数字。我通常会选择Regex.Match作为开始。如果要解析多个字符串,那么通过实例化Regex对象并使用Matches实例方法而不是使用静态匹配方法,您将获得一点好处。这只是一个小小的好处但是嘿 如果你有显著的性能问题,并且输入字符串的格式是静态的,你甚至可能会考虑放弃ReGEX的支持字符串。子串,甚至String。S拆开。您

如何使用正则表达式从以下字符串中获取数值

   AD  .6547 BNM
结果应该是

 .6547
应该这样做和一些c#代码:

试一试


它将选择“.”及其后面的所有数字。

我通常会选择Regex.Match作为开始。如果要解析多个字符串,那么通过实例化Regex对象并使用Matches实例方法而不是使用静态匹配方法,您将获得一点好处。这只是一个小小的好处但是嘿

如果你有显著的性能问题,并且输入字符串的格式是静态的,你甚至可能会考虑放弃ReGEX的支持字符串。子串,甚至String。S拆开。您的问题促使我衡量了几个不同方法之间的性能差异,代码如下

static void TestParse()
    {
        List<string> strList = new List<string>
        {
            "AD  .1234 BNM", 
            "AD  .6547 BNM", 
            "AD  .6557 BNM", 
            "AD  .6567 BNM", 
            "AD  .6577 BNM", 
            "AD  .6587 BNM", 
            "AD  .6597 BNM", 
            "AD  .6540 BNM", 
            "AD  .6541 BNM", 
            "AD  .6542 BNM"
        };

        Stopwatch stopwatch = new Stopwatch();
        string result = "";
        stopwatch.Start();
        for (int i=0; i<100000; i++)
            foreach (string str in strList)
            {
                var match = Regex.Match(str, @"(\.\d+)");
                if (match.Success)
                    result = match.Groups[1].Value;
            }
        stopwatch.Stop();
        Console.WriteLine("\nTotal Regex.Match time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);

        result = "";
        stopwatch.Reset();
        stopwatch.Start();
        Regex exp = new Regex(@"(\.\d+)", RegexOptions.IgnoreCase);
        for (int i = 0; i < 100000; i++)
            foreach (string str in strList)
                result = exp.Matches(str)[0].Value;
        stopwatch.Stop();
        Console.WriteLine("Total Regex object time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);

        result = "";
        stopwatch.Reset();
        stopwatch.Start();
        for (int i = 0; i < 100000; i++)
            foreach (string str in strList)
                result = str.Substring(4, 5);
        stopwatch.Stop();
        Console.WriteLine("Total string.Substring time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);

        result = "";
        stopwatch.Reset();
        stopwatch.Start();
        char[] seps = { ' ' };
        for (int i = 0; i < 100000; i++)
            foreach (string str in strList)
                result = str.Split(seps, StringSplitOptions.RemoveEmptyEntries)[1];
        stopwatch.Stop();
        Console.WriteLine("Total string.Split time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);
    }
static void TestParse()
{
List strList=新列表
{
“公元1234年”,
“公元6547亿英镑”,
“公元6557亿美元”,
“公元6567亿美元”,
“公元6577亿美元”,
“公元6587亿英镑”,
“公元6597亿英镑”,
“公元6540亿美元”,
“公元6541亿美元”,
“公元6542亿英镑”
};
秒表秒表=新秒表();
字符串结果=”;
秒表。开始();

对于(inti=0;i
Regex=newregex(“[0-9.]*)”);Match M=EX.Match(“AD.6547 BNM”);String str=M.Groups[“Z”].Value;
但它不起作用也可以有像
1.234
1.2E34
-0.123
123456.789
123这样的值。
它还可以匹配任何点
foobar.baz
——比如this@RageZ:是的。但是它仍然是强类型的,因为在编译阶段,
csc.exe
决定了什么动作变量是ual类型。因此在IL中,您可以得到特定的类型。@RageZ:是的,这是懒惰开发人员的一个关键字,它也使源代码更具可读性:
var dict=new Dictionary();
vs
Dictionary dict=new Dictionary();
-)我不再做c了,但我记得那些模板化的东西真的很痛苦。
\.\d+
Regex exp = new Regex(
    @"(\.\d+)",
    RegexOptions.IgnoreCase);

string InputText = "AD .1234";
MatchCollection MatchList = exp.Matches(InputText);
Match FirstMatch = MatchList[0];
string value = MatchList[0].value;
\.\d*
static void TestParse()
    {
        List<string> strList = new List<string>
        {
            "AD  .1234 BNM", 
            "AD  .6547 BNM", 
            "AD  .6557 BNM", 
            "AD  .6567 BNM", 
            "AD  .6577 BNM", 
            "AD  .6587 BNM", 
            "AD  .6597 BNM", 
            "AD  .6540 BNM", 
            "AD  .6541 BNM", 
            "AD  .6542 BNM"
        };

        Stopwatch stopwatch = new Stopwatch();
        string result = "";
        stopwatch.Start();
        for (int i=0; i<100000; i++)
            foreach (string str in strList)
            {
                var match = Regex.Match(str, @"(\.\d+)");
                if (match.Success)
                    result = match.Groups[1].Value;
            }
        stopwatch.Stop();
        Console.WriteLine("\nTotal Regex.Match time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);

        result = "";
        stopwatch.Reset();
        stopwatch.Start();
        Regex exp = new Regex(@"(\.\d+)", RegexOptions.IgnoreCase);
        for (int i = 0; i < 100000; i++)
            foreach (string str in strList)
                result = exp.Matches(str)[0].Value;
        stopwatch.Stop();
        Console.WriteLine("Total Regex object time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);

        result = "";
        stopwatch.Reset();
        stopwatch.Start();
        for (int i = 0; i < 100000; i++)
            foreach (string str in strList)
                result = str.Substring(4, 5);
        stopwatch.Stop();
        Console.WriteLine("Total string.Substring time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);

        result = "";
        stopwatch.Reset();
        stopwatch.Start();
        char[] seps = { ' ' };
        for (int i = 0; i < 100000; i++)
            foreach (string str in strList)
                result = str.Split(seps, StringSplitOptions.RemoveEmptyEntries)[1];
        stopwatch.Stop();
        Console.WriteLine("Total string.Split time 1000000 parses: {0}", stopwatch.ElapsedMilliseconds);
    }