gpt4 book ai didi

c# - 也许我需要一个正则表达式?

转载 作者:行者123 更新时间:2023-11-30 13:50:37 26 4
gpt4 key购买 nike

我正在为家庭项目制作一个简单的控制台应用程序。基本上,它会监视一个文件夹中是否有正在添加的任何文件。

FileSystemWatcher fsw = new FileSystemWatcher(@"c:\temp");
fsw.Created += new FileSystemEventHandler(fsw_Created);

bool monitor = true;

while (monitor)
{
fsw.WaitForChanged(WatcherChangeTypes.Created, 1000);
if(Console.KeyAvailable)
{
monitor = false;
}
}

Show("User has quit the process...", ConsoleColor.Yellow);

当新文件到达时,会调用“WaitForChanges”,然后我就可以开始工作了。

我需要做的是检查文件名中的模式。在现实生活中,我将视频文件放入此文件夹中。根据文件名,我将制定规则,将文件移动到特定目录中。所以现在,我将有一个 KeyValue 对列表......包含一个 RegEx(我想?)和一个文件夹。因此,如果文件名与正则表达式匹配,它会将其移动到相关文件夹中。

文件名的例子是:

CSI- NY.S07E01.The 34th Floor.avi

所以,我的正则表达式需要查看它,看看 CSI“AND”(NY“OR”NewYork“OR”New York)是否存在。如果他们这样做,我会将它们移动到 \Series\CSI\NY\ 文件夹。

我需要 AND,因为不同系列的另一个文件示例是:

CSI- Crime Scene Investigation.S11E16.Turn On, Tune In, Drop Dead

所以,对于这个,我需要一些 NOT。所以,我需要检查文件名是否有 CSI,但不是(“New York”或“NY”或“NewYork”)

有人可以帮助我处理这些正则表达式吗?或者,也许有更好的方法?

最佳答案

您可以尝试将条件存储在Func<string,bool>

Dictionary<Func<string,bool>,string> dic = new Dictionary<Func<string, bool>, string>();
Func<string, bool> f1 = x => x.Contains("CSI") && ((x.Contains("NY") || x.Contains("New York"));

dic.Add(f1,"C://CSI/");

foreach (var pair in dic)
{
if(pair.Key.Invoke("CSI- NY.S07E01.The 34th Floor.avi"))
{
// copy
return;
}
}

关于c# - 也许我需要一个正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6000418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com