简体   繁体   English

如何修改此正则表达式,以便它只匹配目录并排除文件?

[英]How to modify this regex so that it only matches directories and excludes files?

This is the regex pattern: ^\\D:(\\\\\\w.+(\\\\?))+$ 这是正则表达式模式: ^\\D:(\\\\\\w.+(\\\\?))+$

〇 Ideal matches (expected): 〇理想匹配(预期):
C:\\folder1\\folder2 C:\\文件夹1 \\文件夹2
C:\\folder1\\folder2\\ C:\\文件夹1 \\文件夹2 \\

✖ But it also matches (actual): ✖但它也匹配(实际):
C:\\folder1\\folder2\\file1.txt C:\\文件夹1 \\文件夹2 \\ FILE1.TXT
C:\\folder1\\folder2\\file2.docx C:\\文件夹1 \\文件夹2 \\ file2.docx

Question (so that it matches the OP title): 问题 (以便与OP标题匹配):
How should I change this regex so it includes files as mentioned in the above file examples? 我应该如何更改此正则表达式,以便它包含上述文件示例中提到的文件?

OR 要么

Subquestion: Do you have any suggestions as an alternative to using Regex? 子问题:作为替代使用正则表达式,您有任何建议吗? Please refer below for details. 请参阅下面的详细信息。

Detailed scenario: 详细方案:
In my WinForms app, I have a TextBox wherein its value is being loaded from a configuration file. 在我的WinForms应用程序中,我有一个TextBox ,其值从配置文件加载。 Values can by anything - integers, strings, and even directories. 值可以是任何东西 - 整数,字符串甚至目录。 If it looks like a directory (this is why I used regex), I want to validate this value by using System.IO.Directory.Exist() . 如果它看起来像一个目录(这就是我使用正则表达式的原因),我想通过使用System.IO.Directory.Exist()来验证这个值。

My guess is that maybe you are trying to write an expression somewhat similar to: 我的猜测是,你可能正在尝试编写一个类似于以下内容的表达式:

^\D:(\\\w+\\\w+(\\?))$ 

or maybe not, I'm not so sure. 或许不是,我不太确定。

Test 测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"^\D:(\\\w+\\\w+(\\?))$";
        string input = @"C:\folder1\folder2
C:\folder1\folder2\
C:\folder1\folder2\file1.txt
C:\folder1\folder2\file2.docx

";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com . 如果您希望探索/简化/修改表达式,请在regex101.com的右上方面板中进行说明 If you'd like, you can also watch in this link , how it would match against some sample inputs. 如果您愿意,您还可以在此链接中查看它与某些示例输入的匹配情况。


RegEx Circuit RegEx电路

jex.im visualizes regular expressions: jex.im可视化正则表达式:

在此输入图像描述

Because file1.txt as well as file2.docx are valid folder names, a better approach would be just to do a simple regex check: 因为file1.txtfile2.docx是有效的文件夹名称,更好的方法是进行简单的正则表达式检查:

(?i)^[A-Z]:\\

If input string matches this regex we consider that input string is a file system path and then you can try/catch System.IO.Directory.Exist() to actually check if provided path is an existing directory. 如果输入字符串与此正则表达式匹配,我们认为输入字符串是文件系统路径,然后您可以尝试/ catch System.IO.Directory.Exist()来实际检查提供的路径是否是现有目录。

Your Regex: 你的正则表达式:

^\\D:(\\\\\\w+(\\\\?))+$

It would take any kind of path without the files. 没有文件,它将采取任何类型的路径。

Example the Result: 结果示例:

Accepted > D:\Folder1\Folder2
Accepted > D:\Folder1\Folder2\
Not Accepted > D:\Folder1\Folder2\text.txt
Accepted > D:\Folder1\Folder2\Folder3
Accepted > D:\Folder1\Folder2\Folder3\

and with more folder is also accepted like the example. 并且像示例一样接受更多文件夹。

To include the files, just add .* like this: 要包含文件,只需添加.*如下所示:

^\\D:(\\\\\\w+(\\\\?)).* without any specific folder name, this one is not efficient because with only this .* , you also get the same result. ^\\D:(\\\\\\w+(\\\\?)).*没有任何特定的文件夹名称,这个没有效率,因为只有这个.* ,你也得到相同的结果。

^\\D:(\\\\A\\\\).* with first specific folder name and the whatever folder name and files. ^\\D:(\\\\A\\\\).*带有第一个特定文件夹名称和任何文件夹名称和文件。

^\\D:\\\\\\w+\\\\\\w+\\\\?\\w+.\\..* with 2 folder is a must then follow by files. ^\\D:\\\\\\w+\\\\\\w+\\\\?\\w+.\\..*含2个文件夹是必须然后按文件。

Simulation Here Regex101 模拟这里Regex101

Switch regex version to see other pattern or copy paste the regex to test. 切换正则表达式版本以查看其他模式或复制粘贴正则表达式进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM