How to find some different words in a string

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello
I have a code
And logicaly I want to write
C#:
string pattern = @"\b(TeNam, Trrr, Tormnew)\b";
to find TeNam or Trrr or Tormnew in a string, but I dont know how to do it syntaxicaly

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {                
            string rootFolder = @"C:\Users\lavraschuk\Desktop\Tdd\Test";
            string pattern = @"\b(TeNam)\b";

            foreach (var file in Directory.EnumerateFiles(rootFolder, "*.txt", SearchOption.AllDirectories))
            {
                using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (Regex.IsMatch(line, pattern, RegexOptions.IgnoreCase))
                            Console.WriteLine(file+ " " + line);
                    }
                }                
            }

            Console.ReadKey();
        }
    }
}

Could you help me?
 
Last edited by a moderator:
This is the second thread where I've cleaned up virtually the same code. Please don't post code containing great wads of whitespace and also please make sure that your braces are aligned. There's no reason to post code that inherently makes it hard for us to read it.
 
Hello
I have a code
And logicaly I want to write
C#:
string pattern = @"\b(TeNam, Trrr, Tormnew)\b";
to find TeNam or Trrr or Tormnew in a string, but I dont know how to do it syntaxicaly

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {               
            string rootFolder = @"C:\Users\lavraschuk\Desktop\Tdd\Test";
            string pattern = @"\b(TeNam)\b";

            foreach (var file in Directory.EnumerateFiles(rootFolder, "*.txt", SearchOption.AllDirectories))
            {
                using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (Regex.IsMatch(line, pattern, RegexOptions.IgnoreCase))
                            Console.WriteLine(file+ " " + line);
                    }
                }               
            }

            Console.ReadKey();
        }
    }
}

Could you help me?
you can use Regular Expression to extract all the words to to a list, say wordsList, then use Distinct() extension to get only distinct words
eg distinctWords().Distinct()
 

Latest posts

Back
Top Bottom