Compare a text file with a text template in C #?

I compared a text file with a template like "..", and I need to write only individual values ​​in a text file ...

while ((line = file.ReadLine()) != null)
{
    foreach (Match match in Regex.Matches(line, @"(\w*)\.\."))
    {
        dest.WriteLine(match.Groups[1]);
    }

    counter++;
}

How to get great value ... Any suggestion?

+3
source share
3 answers

Add matches to the list if they have not been added yet? Or just keep a list of what has already been added? Sort of:

List<string> seen = new List<string>();
    string line = string.Empty;
    while ((line = file.ReadLine()) != null)
    {
        foreach (Match match in Regex.Matches(line, @"(\w*)\.\."))
        {
            if (!seen.Contains(line))
            {
                Console.WriteLine(line);
                seen.Add(line);
            }
        }
    }

edit: I realized that you were here; if you really need the value of the match group, replace the line in the conditional block with match.Groups [1] .Value ...

+3
source

I recommend inserting your values ​​into an array and using this to ignore duplicates:

ArrayList myValues = new ArrayList();

while ((line = file.ReadLine()) != null)
{
  foreach (Match match in Regex.Matches(line, @"(\w*)\.\."))
  {
    if(!myValues.Contains(match.Groups[1])) {
         myValues.Add(match.Groups[1]);
         dest.WriteLine(match.Groups[1]);
    }
  } 
  counter++;
}
0
source

match.Group [1] HashSet?

HashSet<string> hs = new HashSet<string>();
string line = "insert into depdb..fin_quick_code_met..jjj..depdb..jjj";
foreach (Match match in Regex.Matches(line, @"(\w*)\.\."))
{
    hs.Add(match.Groups[1].ToString());
}
foreach (string item in hs)
    Console.WriteLine(item);
0

Source: https://habr.com/ru/post/1780621/


All Articles