0

string

zf3kabxcde224lkzf3mabxc51+crsdtzf3nab=

with a specified pattern length of 3, the method should return the pattern abx with an occurrence value of two, and zf3 with an occurrence value of three.

3
  • Regular expressions can see if something is repeated some minimum amount of times, but they can't count. Commented Mar 27, 2019 at 6:48
  • You can do instead Regex.Matches("zf3kabxcde224lkzf3mabxc51+crsdtzf3nab=", "abx").Count Commented Mar 27, 2019 at 6:52
  • 1
    Your question needs clarification. Why is it choosing to return abx with two, and zf3 with three, and yet it is not returning bxc with two or nab with one (among other examples I could give). Commented Mar 27, 2019 at 6:55

4 Answers 4

3

I suggest using Linq instead of regular expressions, e.g.:

  string source = @"zf3kabxcde224lkzf3mabxc51+crsdtzf3nab=";

  int size = 3;

  var result = Enumerable
    .Range(0, source.Length - size + 1)
    .GroupBy(i => source.Substring(i, size))
    .Where(chunk => chunk.Count() > 1)
    .Select(chunk => $"'{chunk.Key}' appears {chunk.Count()} times");

 Console.Write(string.Join(Environment.NewLine, result));

Outcome:

'zf3' appears 3 times
'abx' appears 2 times
'bxc' appears 2 times

Please, note, that we have in fact two different chunks (abx and bxc) which appear twice.

Linq is very flexible, so you can easily make a query in a different way, e.g.

 var result = Enumerable
    .Range(0, source.Length - size + 1)
    .GroupBy(i => source.Substring(i, size))
    .Where(chunk => chunk.Count() > 1)
    .GroupBy(chunk => chunk.Count(), chunk => chunk.Key)
    .OrderBy(chunk => chunk.Key)
    .Select(chunk => $"Appears: {chunk.Key}; patterns: {string.Join(", ", chunk)}");

 Console.Write(string.Join(Environment.NewLine, result));

Outcome:

 Appears: 2; patterns: abx, bxc
 Appears: 3; patterns: zf3
Sign up to request clarification or add additional context in comments.

Comments

2

I think it's not good task for regex, I would use dictionary with splitting input string to strinns of specified length:

var length = 3;
var str = "zf3kabxcde224lkzf3mabxc51+crsdtzf3nab=";
var occurences = new Dictionary<string, int>();
for (int i = 0; i < str.Length - length + 1; i++)
{
    var s = str.Substring(i, length);
    if (occurences.ContainsKey(s))
      occurences[s] += 1;
    else
      occurences.Add(s, 1);
}

Now you can check how many occurences has any string of length 3, eg.: occurences["zf3"] equals 3.

Comments

0

Simplest solution:

   var myString = "zf3kabxcde224lkzf3mabxc51+crsdtzf3nab=";
        var length = 3;

        for (int i = 0; i < myString.Length - length + 1; i++)
        {
               var Pattern = myString.Substring(i, length).Replace("+",".+").Replace("*", ".*").Replace("?", ".?");
            var Occurrence = Regex.Matches(myString, Pattern).Count;

            Console.WriteLine(Pattern + " : " + Occurrence);
        }

2 Comments

Have tried it is throwing an error System.ArgumentException: 'parsing '+cr' - Quantifier {x,y} following nothing.'
You have to search for the ".+" instead of "+". Same goes for "*" and "?"
0
        var content = "zf3kabxcde224lkzf3mabxc51+crsdtzf3nab=";
        var patternLength = 3;            
        var patterns = new HashSet<string>();

        for (int i = 0; i < content.Length - patternLength + 1; i++)
        {
            var pattern = content.Substring(i, patternLength);                
            var Occurrence = Regex.Matches(content, pattern.Replace("+", @"\+")).Count;
            if (Occurrence > 1 && !patterns.Contains(pattern))
            {
                Console.WriteLine(pattern + " : " + Occurrence);
                patterns.Add(pattern);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.