C# 使用正则表达式模式查找列表中是否存在项

C# 使用正则表达式模式查找列表中是否存在项,c#,regex,list,C#,Regex,List,我有一个附加了时间戳的文件列表,还有一个包含正则表达式模式的列表。我想验证“refFiles”中的所有模式是否存在于“files”中 下面是示例文件 List<string> files = new List<string>(); files.Add("AB_DBER_2016101194814.txt"); files.Add("AB_EBER_2016101194815.txt"); files.Add("AB_FBER_2016101194811.txt"); 我

我有一个附加了时间戳的文件列表,还有一个包含正则表达式模式的列表。我想验证“refFiles”中的所有模式是否存在于“files”中

下面是示例文件

List<string> files = new List<string>();
files.Add("AB_DBER_2016101194814.txt");
files.Add("AB_EBER_2016101194815.txt");
files.Add("AB_FBER_2016101194811.txt");

我想知道如何在第二个列表中找到正则表达式模式,以验证第一个列表中是否存在匹配的模式

注意对正则表达式的更改。它在文本
@
字符串中使用
.
而不是
;这将匹配一个周期

在这两种情况下,这都是输出:

AB_DBER_2016101194814.txt
AB_EBER_2016101194815.txt
AB_FBER_2016101194811.txt
这个代码对我有用

        var files = new List<string>
        {
            "AB_DBER_2016101194814.txt",
            "AB_EBER_2016101194815.txt",
            "AB_FBER_2016101194811.txt"
        };

        var refFiles = new List<string>
        {
            "AB_DBER_[0-9]{13,13}.txt",
            "AB_EBER_[0-9]{13,13}.txt",
            "AB_FBER_[0-9]{13,13}.txt"
        };

        foreach (var patternFile in refFiles)
        {
            var regularExp = new Regex(patternFile);
            foreach (var file in files)
            {
                if (regularExp.IsMatch(file))
                {
                    Console.WriteLine(file);
                }
            }
        }
var文件=新列表
{
“AB_DBER_2016101194814.txt”,
“AB_EBER_2016101194815.txt”,
“AB_FBER_2016101194811.txt”
};
var refFiles=新列表
{
“AB_DBER_[0-9]{13,13}.txt”,
“AB_EBER_[0-9]{13,13}.txt”,
“AB_FBER_[0-9]{13,13}.txt”
};
foreach(refFiles中的var patternFile)
{
var regularExp=新的正则表达式(patternFile);
foreach(文件中的var文件)
{
if(regularExp.IsMatch(文件))
{
Console.WriteLine(文件);
}
}
}

到底是什么问题?你不知道怎么做正则表达式?或者?我想知道如何在第二个列表中找到正则表达式模式,以验证第一个列表中是否存在匹配的模式,
[0-9]{13,13}
=>
[0-9]{13}
=>
\d{13}
,我猜您应该在扩展名
\.txt
之前转义该句点(转义的反斜杠不是逐字字符串)@pinkfloydx33你能告诉我正确的正则表达式应该如何匹配文本AB_DBER_2016101194814.txt吗?我想我会删除我的答案,我们用同样的方法解决了它。:-)有可能避免第二个foreach吗?如果它不是正则表达式,那么我可以在第一个foeach using list.contains?@blue我不知道“如果它不是正则表达式…”是什么意思。在任何情况下,我用LINQ添加了另一个选项,它隐藏了
any
中的第二个循环。否则,应该跳过这些周期“AB_DBER_2016101194848; 14txt”(无文件扩展名且名称不正确)也将match@pinkfloydx33接球不错,更新了。
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        List<string> files = new List<string>();
        files.Add("AB_DBER_2016101194814.txt");
        files.Add("AB_EBER_2016101194815.txt");
        files.Add("AB_FBER_2016101194811.txt");

        // the following will not match
        files.Add("AB_FBER_20161011948111txt");
        files.Add("This_Does_Not_Match.txt");

        List<string> refFiles = new List<string>();
        refFiles.Add(@"AB_DBER_[0-9]{13,13}\.txt");
        refFiles.Add(@"AB_EBER_[0-9]{13,13}\.txt");
        refFiles.Add(@"AB_FBER_[0-9]{13,13}\.txt");
        foreach (var pattern in refFiles)
        {
            var regex = new Regex(pattern);
            foreach (var file in files)
            {
                if (regex.IsMatch(file))
                {
                    Console.WriteLine(file);
                }
            }
        }
    }
}
foreach (var file in files)
{
    if (refFiles.Any(pattern => Regex.IsMatch(file, pattern)))
    {
        Console.WriteLine(file);
    }
}
AB_DBER_2016101194814.txt
AB_EBER_2016101194815.txt
AB_FBER_2016101194811.txt
        var files = new List<string>
        {
            "AB_DBER_2016101194814.txt",
            "AB_EBER_2016101194815.txt",
            "AB_FBER_2016101194811.txt"
        };

        var refFiles = new List<string>
        {
            "AB_DBER_[0-9]{13,13}.txt",
            "AB_EBER_[0-9]{13,13}.txt",
            "AB_FBER_[0-9]{13,13}.txt"
        };

        foreach (var patternFile in refFiles)
        {
            var regularExp = new Regex(patternFile);
            foreach (var file in files)
            {
                if (regularExp.IsMatch(file))
                {
                    Console.WriteLine(file);
                }
            }
        }