Pregunta

Tengo un IEnumerable que quiero filtrar hacia abajo utilizando una gran variedad de expresiones regulares para encontrar las posibles coincidencias. He estado tratando de unirse a mi directorio y expresiones regulares cadenas utilizando LINQ, pero parece que no puede hacerlo bien. Esto es lo que estoy tratando de hacer ...

string[] regexStrings = ... // some regex match code here.

// get all of the directories below some root that match my initial criteria.
var directories = from d in root.GetDirectories("*", SearchOption.AllDirectories)
                  where (d.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0
                        && (d.GetDirectories().Where(dd => (dd.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).Count() > 0// has directories
                            || d.GetFiles().Where(ff => (ff.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0).Count() > 0) // has files)
                  select d;

// filter the list of all directories based on the strings in the regex array
var filteredDirs = from d in directories
                   join s in regexStrings on Regex.IsMatch(d.FullName, s)  // compiler doesn't like this line
                   select d;

... alguna sugerencia sobre cómo puedo conseguir que esto funcione?

¿Fue útil?

Solución

Si sólo desea directorios que responden a todas las expresiones regulares.

var result = directories
    .Where(d => regexStrings.All(s => Regex.IsMatch(d.FullName, s)));

Si sólo desea directorios que coincidan con al menos uno de expresiones regulares.

var result = directories
    .Where(d => regexStrings.Any(s => Regex.IsMatch(d.FullName, s)));

Otros consejos

No creo que el enfoque que está tomando es exactamente lo que quiere de todos modos. Que comprobará el nombre contra TODAS las cadenas de expresiones regulares, en lugar de un cortocircuito en el primer partido. Además, sería duplicar los directorios si se igualaron más de un patrón.

Creo que quieres algo como:

string[] regexStrings = ... // some regex match code here.

// get all of the directories below some root that match my initial criteria.
var directories = from d in root.GetDirectories("*", SearchOption.AllDirectories)
                  where (d.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0
                        && (d.GetDirectories().Where(dd => (dd.Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).Count() > 0// has directories
                            || d.GetFiles().Where(ff => (ff.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0).Count() > 0) // has files)
                  select d;

// filter the list of all directories based on the strings in the regex array
var filteredDirs = directories.Where(d =>
    {
        foreach (string pattern in regexStrings)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(d.FullName, pattern))
            {
                return true;
            }
        }

        return false;
    });

se echa en falta su palabra clave Dónde frente al unirse a

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top