0

I have some code to find a set of character (or any) occur in each word in some sentences. Ex: sentences: "existing pessimist optimist this is" character: "is" number of occurrences: 1 (give 2 in VS , give 3 in regex101); 2(give 2 in VS,give 2 in regex101); 3(give 2 in VS,give 1 in regex101). I don't understand what I do. Here is my code

    Regex regex;
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.Append("([a-zA-Z]*[string]{numberOccur,}[^\\s][a-zA-Z]+\\s)");
    //Console.Write("Please input number of sentence: ");
    int NumerofSentence = Int32.Parse(Console.ReadLine());//Number of sentence want to check
    List<string> ListSentence = new List<string>();
    Console.Write("Input sentence: ");
    for (int index = 0; index < NumerofSentence; index++)
    {
        ListSentence.Add(Console.ReadLine());//Store in a collection object
    }
    //Console.Write("Input number of occurrences character in each word in sentence separated by non-word characters: ");
    int NumberOccur = Int32.Parse(Console.ReadLine());
    //Console.Write("Input character you want to find in all sentence: ");
    string stringFinded = Console.ReadLine();
    for (int index = 0; index < NumerofSentence; index++)
    {
        stringBuilder.Replace("string", stringFinded);
        stringBuilder.Replace("numberOccur", NumberOccur.ToString());
        regex = new Regex(stringBuilder.ToString());
        Match match = regex.Match(ListSentence[index]);
        if (match.Success)
        {
            Console.WriteLine(@"Sentences: {0}  
            Character: {1} 
            Number of occurrences in each sentences:   {2}",ListSentence[index],stringFinded,match.Groups.Count);
        }
        else
        {
            Console.WriteLine("Not found " );
        }
    }
delv
  • 1
  • 3
  • You have highlighted 5 `is` in that sentence. Why is the result not 5? And how do you get 3 different results in Regex101 alone? And another 3 results in VS? Shouldn't the result at least stay consistent across the tools? – Thomas Weller Jun 28 '18 at 17:17
  • Please reduce the code ao a [mcve]. Remove all user input and use hard coded values to demonstrate your problem. – Thomas Weller Jun 28 '18 at 17:19
  • Make sure you're using all the same flags in your code that you're using on regex101. I've spent more time than I'd like to admit on debugging a working regex because I forgot the multiline flag. – emsimpson92 Jun 28 '18 at 17:20
  • You can see Regex Pattern i writed, if match it will return 3 not 5. I try to test 3 time in Vs 2013 and 3 time in Regex101, I see the result in Regex101 map which I think. After, I see my code, I think it will wrong but I don't know where is it – delv Jun 28 '18 at 17:21
  • @emsimpson92 can I set Flag in ReGex in Visual Studio 2013 – delv Jun 28 '18 at 17:24
  • @ThomasWeller, Yes, I haven done – delv Jun 28 '18 at 17:30
  • You can set flags a couple different ways. The easiest is probably to include the optional `RegexOptions` parameter, but you can also add something like `(?m)` to the beginning of your pattern for multi line enabled, `(?i)` for case insensitivity, etc... – emsimpson92 Jun 28 '18 at 17:32
  • For the `"([a-zA-Z]*[string]{numberOccur,}[^\\s][a-zA-Z]+\\s)"` pattern, you do not need `(?m)`, there is no `^` and `$` anchors. – Wiktor Stribiżew Jun 28 '18 at 17:34
  • The thing is you need to count the number of matches retrieved with `Regex.Matches`. Regex.Match only find the first match, and `.Groups` contain number of capturing groups in the regex + 1. – Wiktor Stribiżew Jun 28 '18 at 18:04

0 Answers0