-1

我得到了以下格式的字符串:

ASDF [         6]

ZXC[1]

OtPasd[ 4 ]

asdffa[   7]

我需要检索有效字符串括号之间的整数。只要满足以下条件,这些字符串就有效:

  1. 括号之间仅存在空格。IE:“ZXCV[a2]”无效
  2. 所有支架均已正确闭合。IE:“qwr[2”无效
  3. 所有字符串都只有一个开/关括号。IE:“zxcf[4]]]”无效

我最好避免使用正则表达式,因为我得到了大量的字符串,所以计算量不大的东西会更可取。

验证和检索整数的最干净和最快的方法是什么?

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

4

5 回答 5

1

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);

And here are the results:

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.

于 2012-09-11T14:14:41.740 回答
0

如果您想避免使用正则表达式...使用 IndexOf/LastIndexOf 然后解析剩余的字符串是否适合您的需要?

于 2012-09-11T13:32:58.640 回答
0

要将 int 放在括号之间,您也可以尝试这种方式:

string tmpString = "ASDF [         6]";
int start = tmpString.IndexOf('[') + 1;
int length = tmpString.IndexOf(']') - start;
string subString = tmpString.Substring(start, length);
int tempInt;
if(Int.TryParse(subString, out tempInt))
return tempInt;
于 2012-09-11T13:36:32.667 回答
0

试试这个正则表达式:

\[\s*(\d+)\s*\]$
于 2012-09-11T13:29:44.020 回答
0

在匹配组中使用这个正则表达式(?m)(?!<=\[)(\[\s*)(\d+)(\s*\])(?!\]) 你的整数

于 2012-09-11T13:29:58.710 回答