none
Get distinct words from a file c# RRS feed

  • Question

  • Hey!

    I need help to get all distinct words that starts with Momentum_ in the beginning of the word.
                foreach (var f in files)
                {
                    string content = File.ReadAllText(f);
    
                    string pattern = "Momentum_";
                    MatchCollection matches = Regex.Matches(content, pattern, RegexOptions.Singleline);
                    foreach (Match match in matches)
                    {
                        MessageBox.Show(match.Groups["Momentum_"].ToString());
                    }
                    Console.ReadLine();
                    //if (content.Contains(HittaTxt.Text))
                    //{
                    //    content = content.Replace(HittaTxt.Text, ErsättTxt.Text);
                    //    File.WriteAllText(f, content);
    
                    //}
                }

    Any ideas? Thankful for help!
    Thursday, November 14, 2019 11:34 AM

Answers

  • Thanks but i dont know how to implement that code in my..? I have multiple files and want distinct of them all..

    A I mentioned, use ReadAllLines for each file in a foreach e.g.

                var wordResults = new List<string>();
    
                foreach (var file in files)
                {
                    var wordList =
                    (
                        from line in File.ReadAllLines(file)
                        from Match match in Regex.Matches(line, @"(?<!\w)Momentum_\w+")
                        select match.Value
                    ).ToList();
    
                    
                    wordResults.AddRange(wordList);
                }
    
                var distinctWords  = wordResults.Distinct();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Carlo Goretti Friday, November 15, 2019 8:18 AM
    Thursday, November 14, 2019 3:22 PM
    Moderator

All replies

  • Hello,

    Here is an example that mimics lines from a single file where you would use File.ReadAllLines in your app where the result would be a variable lines.

    // mimic lines in a single file
    var lines = new[]
    {
        "Lorem ipsum Momentum_Karen Second Momentum_Karen lorem ipsum.  Momentum_111 Something else now.",
        "Momentum_Karen Momentum_11",
        "_Karen 11",
        "Momentum_Karen Momentum_12"
    };
    
    var wordList = 
        (
            from line in lines
            from Match match in Regex.Matches(line, @"(?<!\w)Momentum_\w+")
            select match.Value
        ).ToList();
    
    var distinctWords = wordList.Distinct().ToArray();
    
    Console.WriteLine(string.Join("\n",distinctWords));

    Results

    Momentum_Karen
    Momentum_111
    Momentum_11
    Momentum_12


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 14, 2019 12:34 PM
    Moderator
  • Thanks but i dont know how to implement that code in my..? I have multiple files and want distinct of them all..
    Thursday, November 14, 2019 1:55 PM
  • Thanks but i dont know how to implement that code in my..? I have multiple files and want distinct of them all..

    A I mentioned, use ReadAllLines for each file in a foreach e.g.

                var wordResults = new List<string>();
    
                foreach (var file in files)
                {
                    var wordList =
                    (
                        from line in File.ReadAllLines(file)
                        from Match match in Regex.Matches(line, @"(?<!\w)Momentum_\w+")
                        select match.Value
                    ).ToList();
    
                    
                    wordResults.AddRange(wordList);
                }
    
                var distinctWords  = wordResults.Distinct();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Carlo Goretti Friday, November 15, 2019 8:18 AM
    Thursday, November 14, 2019 3:22 PM
    Moderator
  • Hi Carlo,

    Thank you for posting here.

    According to your description, you seem to want to extract some words from different files based on different keywords.

    I created a WinForm program based on Karen’s code, here is the code.

            private void Button1_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        String path = openFileDialog.FileName;
                        textBox1.Text = path;
                    }
                }
            }
            private void Button2_Click(object sender, EventArgs e)
            {
               String[] lines =  File.ReadAllLines(textBox1.Text);
                var wordList =
                            (
                                from line in lines
                                from Match match in Regex.Matches(line, $@"(?<!\w){textBox2.Text}\w+")
                                select match.Value
                            ).ToList();
    
                var distinctWords = wordList.Distinct().ToArray();
                string result = string.Join("\n", distinctWords);
                MessageBox.Show(result);
            }
    

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 15, 2019 6:48 AM
  • Do you know how i can ignore case?
    Best regards
    Friday, November 15, 2019 8:19 AM
  • Do you know how i can ignore case?
    Best regards

    (?i) within the pattern begins case-insensitive matching, (?-i) ends it.

    Here is the revised code.

    string[] files = {"textFile1.txt", "textFile2.txt" };
    var wordResults = new List<string>();
    var findThis = "Momentum_";
    
    foreach (var file in files)
    {
        var wordList =
        (
            from line in File.ReadAllLines(file)
            from Match match in Regex.Matches(line, $@"(?<!\w)(?i){findThis}\w+")
            select match.Value
        ).ToList();
    
    
        wordResults.AddRange(wordList);
    }
    
    var distinctWords = wordResults.Distinct().ToArray();
    foreach (var distinctWord in distinctWords)
    {
        Console.WriteLine(distinctWord);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 15, 2019 10:39 AM
    Moderator