一个正则反对性或LINQ代替当前功能


    //=========================================================================
    ///This is the main function 
    /// Convert String to array of string patterns
    /// Clean the new array from any redundances 
    /// Get the repeated items and their repeated numbers 
    /// </summary> 
    /// <param name="source">Original string that was generated</param> 
    /// <param name="repeatedNumber">how many items were found</param> 
    /// <param name="sequenceLength">the user input to create the string patterns</param> 
    /// <returns>the list of repeated items and their repeated numbers </returns> 
    static List<MyDataClass> GetConsecutiveChars(string source, out int repeatedNumber, int sequenceLength)
    {
        //var matchList = Regex.Matches(source, "([a-zA-Z0-9\+\-\*\&\^\%\$\#\@\!])\1{" + (sequenceLength - 1) + "}").Cast<Match>()
        //                                                                                 .Select(m => m.Value)
        //                                                                                 .ToArray();
        ////var matchList = Regex.Matches(source, "([a-zA-Z0-9])\1{" + (sequenceLength - 1) + "}").Cast<Match>()
        ////                                                                                 .Select(m => m.Value)
        ////                                                                                 .ToArray();
        //var result2 = source.GroupBy(c => c).Where(c => c.Count() >1).Select(c => new { charName = c.Key, charCount = c.Count() });
        //===========================================================================
        //Devid the source to the pieces :
        List<string> list = DistributedStringToArray(source, sequenceLength);
        //===========================================================================
        //Clean list from duplicated values
        list = (from item in list select item).Distinct().ToList();            
        //===========================================================================
        //Check if it was repeated or not 
        List<MyDataClass> result = FillListWtihRepeatedItems(source, list);
        //===========================================================================
        //Get the number of repeated items 
        repeatedNumber = 0;
        repeatedNumber = result.Count();
        //===========================================================================
        //return the list of repeated items and their repeated numbers
        return result;
    }

我的问题:我可以在一个正则表达式语句或linq statment中执行所有操作吗?

我尝试了,但我做不到。我在正则表达式陈述和linq statment中添加了评论。

请建议我。

我将当前的应用程序上传到https://dotnetfiddle.net/qo7pvs#run-results

示例:

statement is : [I like to know little]

结果:

k = 2
o = 2
i = 2
li = 2
....

试图计算语句中的重复字符或单词2次。

要获得连续的字符,您需要使用所谓的new返回引用。它是这样的工作:

(?<char>w)k<char>

例如,以下内容将找到连续的b S:

var input = "aaaaabbbbbbccccc";
var match = Regex.Match(input, @"(?<char>b)k<char>+");

输出:bbbbbb

可能不是最有效的,但是如果我在linqpad中尝试一下:

Enumerable.Range(1, source.Length - 1)
          .SelectMany(l => Enumerable.Range(0, source.Length - l - 1)
                                     .Select(i => source.Substring(i, l)) )
          .GroupBy(s => s)
          .Where(g => g.Count() > 1 && !g.Key.Any(char.IsWhiteSpace))
          .Select(c => new { c.Key, Count = c.Count() })

我得到:

Key Count
l   2
i   2
k   2
t   3
o   2
li  2

最新更新