【发布时间】:2021-06-17 09:56:15
【问题描述】:
我正在尝试构建一个正则表达式来验证目录路径,但我找不到这样做的好方法。 有人可以帮助我吗? 例子:
/test/ -> could match
/test/toto/tata -> could match
\test\ -> could match
\test\toto\tata -> could match
//test/ -> could not match
\\test\ -> could not match
/test//toto -> could not match
/test\tata -> could not match
编辑:我尝试了这个正则表达式,但它并不匹配所有情况。
^(\/{0,1}(?!\/))[A-Za-z0-9\/\-_]+(\.([a-zA-Z]+))*$
【问题讨论】:
-
这当然取决于所使用的操作系统和文件系统。
-
只测试双斜杠!(line.Contains("//") || line.contains("\\\\"))
-
@KlausGütter 它在 Windows 上
-
@jdweng 我想要一个正则表达式来验证
-
为什么需要 RGEX。仅当无法使用普通字符串方法时才应使用正则表达式。正则表达式不是很有效。一个简单的 if 语句就足够了: if(!(line.Contains("//") || line.contains("\\\\")) )
标签: c# regex validation path