匹配括号之间的整数 [英] Matching an integer between the brackets

查看:159
本文介绍了匹配括号之间的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给出以下格式的一串字符串:

I am given bunch of strings in the following format:

ASDF [         6]

ZXC[1]

OtPasd[ 4 ]

asdffa[   7]

我需要检索的是有效字符串的括号内的整数。该字符串是有效的,只要:

I need to retrieve the integer between the brackets on strings that are valid. The strings are valid as long as:

  1. 只有白色的空间是present括号之间。即:ZXCV [A2]是无效
  2. 所有支架正确关闭。即:QWR [2是无效
  3. 所有字符串有一个开/闭只支架。即:zxcf [4]]]是无效

我会preferably,以避免正则表达式,因为我得到了大量的字符串,使一些计算unintensive将是preferable。

I would preferably like to avoid Regex as I am getting a large number of strings so something computationally unintensive would be preferable.

什么是干净和最快的方式来验证和获取整数?

What is the cleanest and fastest way to validate and retrieve the integer?

编辑:我决定用正则表达式

I decided to use Regex.

推荐答案

在我个人认为最干净的解决方案是使用正则表达式。但猜测,而不是如果是计算密集型我宁愿基准它。这里的code。

In my personal opinion the cleanest solution is to use regexes. But instead of guessing if it is computationally intensive I would rather benchmark it. Here's the code.

const int Count = 10000000;
const string testString = "<whatever>";

// Solution No. 1: use Regex.Match()    
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Count; i++)
{
    var match = Regex.Match(@"\[\s*(\d+)\s*\]$", testString);
    if (!match.Success)
        continue;
    var number = int.Parse(match.Groups[1].Value);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);

// Solution No. 2: use IndexOf() and Substring() shenanigans
sw.Start();
for (int i = 0; i < Count; i++)
{
    var lb = testString.IndexOf('[');
    var rb = testString.LastIndexOf(']');
    if (lb < 0 || rb != testString.Length - 1)
        continue;
    var str = testString.Substring(lb + 1, rb - lb - 1);
    int number;
    if (!int.TryParse(str, out number))
        continue;
    // use the number
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);

和这里的结果:

Solution  |  testString  |   Time (ms)  | Comment
----------|--------------|--------------|-----------------------
     1    | abc [      ] |    4476      | Invalid input string
     2    | abc [      ] |    6594      | Invalid input string
     1    | abc[1234]    |    4446      | Valid input string
     2    | abc[1234]    |    6290      | Valid input string

正如你所看到的,不仅是正则表达式的解决方案更短,更清洁,它实际上是快。如果你有不同的输入字符串玩你会发现,时间越长你输入的字符串是,较大的第一和第二个解决方案之间的差距。

As you can see, not only the regex solution is shorter and cleaner, it is actually faster. And if you play with different input strings you will notice that the longer your input string is, the larger the gap between the first and the second solutions.

这篇关于匹配括号之间的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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