Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Comparando dos carpetas diferentes con Regex en C#

Quiero comparar si un archivo está en dos carpetas diferentes.

 var reg = new Regex(@"TC__\d{3,4}_8exp$"); var reg1 = new Regex(@"TC_\d{3,4}$"); var FileNames_List_Jetzt_Dateien = Directory.GetFiles(@"PATH2", "*.html") .Where(path => reg.IsMatch(Path.GetFileNameWithoutExtension(path))) .Select(Folder_Jetzt_Dateien => Path.GetFileNameWithoutExtension(Folder_Jetzt_Dateien));

De este estoy obteniendo una lista:

 TC__6493_8exp TC__6494_8exp TC__6495_8exp...

De este:

 var TEST_FÄLLE = Directory.GetFiles(@"PATH2", "*.exp") .Where(path => reg1.IsMatch(Path.GetFileNameWithoutExtension(path))) .Select(Folder_Jetzt_Dateien => Path.GetFileNameWithoutExtension(Folder_Jetzt_Dateien));

Estoy recibiendo lista:

 TC_6493 TC_6494 TC_6495...

Para mí, el TC__6493_8exp es lo mismo que el TC_6493 . ¿Cómo usar Intersect para estas dos listas con los mismos elementos?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Aquí hay una manera de escribir el comparador personalizado con una expresión regular:

 public static Regex rx = new Regex(@"^(TC_)_?(\d+).*", RegexOptions.Compiled); public class FilePathComparer : IEqualityComparer<string> { public bool Equals(string x, string y) { return rx.Replace(x, "$1$2") == rx.Replace(y, "$1$2"); } public int GetHashCode(string s) { return rx.Replace(s,"$1$2").GetHashCode(); } }

En el código más adelante, solo use

 var intersected = FileNames_List_Jetzt_Dateien.Intersect(TEST_FÄLLE, new FilePathComparer());

Vea la demostración de C# y la demostración de expresiones regulares .

La expresión regular ^(TC_)_?(\d+).* coincide

  • ^ - comienzo de la cadena
  • (TC_) - Grupo 1 ( $1 ): TC_
  • _? - un guión bajo opcional
  • (\d+) - Grupo 2 ( $2 ): uno o más dígitos
  • .* - el resto de la cadena.
over 4 years ago · Santiago Trujillo Report

0

Puede escribir un comparador personalizado y pasarlo al método Intersect

 using System.Collections.Generic; class FileComparer : IEqualityComparer<string> { const string ext = "_exp8"; public bool Equals(string left, string right) { if (left.EndsWith(ext) && !right.EndsWith(ext)) { return StripExt(left) == right; } else if (right.EndsWith(ext)) { return left == StripExt(right); } return left == right; } private static string StripExt(string s) => s.Substring(0, s.Length - ext.Length); public int GetHashCode(string s) { if (s.EndsWith(ext)) { return StripExt(s).GetHashCode(); } else { return s.GetHashCode(); } } }
 using System; using System.Linq; var files1 = new string[] { "1", "2", "3" }; var files2 = new string[] { "1_ext", "2_exp8", "3_exp8" }; var a = files1.Intersect(files2, new FileComparer()); var res = string.Join(", ", a); Console.WriteLine(res);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!