Matching an integer between the brackets

前端 未结 5 818
有刺的猬
有刺的猬 2021-01-29 07:57

I am given bunch of strings in the following format:

ASDF [         6]

ZXC[1]

OtPasd[ 4 ]

asdffa[   7]

I need to retrieve the integer betwee

相关标签:
5条回答
  • 2021-01-29 08:06

    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.

    0 讨论(0)
  • 2021-01-29 08:06

    If you want to avoid using Regex... Would using IndexOf/LastIndexOf and then parsing the remaining string be suitable for what you need?

    0 讨论(0)
  • 2021-01-29 08:13

    use this regex (?m)(?!<=\[)(\[\s*)(\d+)(\s*\])(?!\]) your integer in match group

    0 讨论(0)
  • 2021-01-29 08:20

    Try this regular expression:

    \[\s*(\d+)\s*\]$
    
    0 讨论(0)
  • 2021-01-29 08:24

    To get the int in between the brackets you can also try this way:

    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;
    
    0 讨论(0)
提交回复
热议问题