Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Regex禁止windows在其文件/文件夹名中不允许使用的所有符号_C#_Regex - Fatal编程技术网

C# Regex禁止windows在其文件/文件夹名中不允许使用的所有符号

C# Regex禁止windows在其文件/文件夹名中不允许使用的所有符号,c#,regex,C#,Regex,windows中文件名或文件夹名中不允许的符号是\/:*?“|。我为它编写了一个正则表达式,但无法编写要排除的正则表达式”(双引号) Regex Regex=new Regex(@“^[\\\/\:\*\?\'\\\\\\\\\\\\]+$”; 按以下方式尝试,但不起作用: Regex regex = new Regex(@"^[\\\/\:\*\?\'\<\>\|&quot;]+$"); Regex Regex=newregex(@“^[\\\/\:\*\?\'\\\\

windows中文件名或文件夹名中不允许的符号是
\/:*?“<>|
。我为它编写了一个正则表达式,但无法编写要排除的正则表达式”(双引号)

Regex Regex=new Regex(@“^[\\\/\:\*\?\'\\\\\\\\\\\\]+$”;
按以下方式尝试,但不起作用:

Regex regex = new Regex(@"^[\\\/\:\*\?\'\<\>\|&quot;]+$");
Regex Regex=newregex(@“^[\\\/\:\*\?\'\\\\\\\\\”]+$”;
编辑:
我特别寻找带有正则表达式本身的代码,因此它不是重复的


非常感谢您的帮助。谢谢。

您可以这样做,而不是使用Regex:

var invalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());
var invalid = input.Any(chr => invalidChars.Contains(chr));
var invalidChars=newhashset(Path.GetInvalidFileNameChars());
var invalid=input.Any(chr=>invalidChars.Contains(chr));

不使用正则表达式,您可以执行以下操作:

var invalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());
var invalid = input.Any(chr => invalidChars.Contains(chr));
var invalidChars=newhashset(Path.GetInvalidFileNameChars());
var invalid=input.Any(chr=>invalidChars.Contains(chr));

@Allrameest答案使用
Path。GetInvalidFileNameChars()
可能是您应该使用的答案

我想说明的是,您的正则表达式实际上是错误的或非常无效的(我不知道您到底想做什么)

因此,使用:

var regex = new Regex(@"^[\\\/\:\*\?\'\<\>\|]+$");
只要找到一个就可以了


更新人@JohnLBevan

var regex = new Regex(
    "[" + Regex.Escape(new string(Path.GetInvalidFileNameChars())) + "]");
if (regex.Match(filename).Success) {
    throw new ArgumentException("Bad filename");
}

(不使用
string.Format(…)
,因为这个正则表达式应该是静态的,并且无论如何都应该预编译)

@Allrameest-answer使用
Path.GetInvalidFileNameChars()
可能是您应该使用的答案

我想说明的是,您的正则表达式实际上是错误的或非常无效的(我不知道您到底想做什么)

因此,使用:

var regex = new Regex(@"^[\\\/\:\*\?\'\<\>\|]+$");
只要找到一个就可以了


更新人@JohnLBevan

var regex = new Regex(
    "[" + Regex.Escape(new string(Path.GetInvalidFileNameChars())) + "]");
if (regex.Match(filename).Success) {
    throw new ArgumentException("Bad filename");
}

(不使用
string.Format(…)
,因为这个正则表达式无论如何都应该是静态的和预编译的)

您不是在寻找
^[^\\\/:*?“|]+$
?假设您想匹配一个不包含这些字符的文件名,为什么不使用
\“
?在C#中,您可以使用一种特殊的方法来检查文件名是否有效。不需要使用正则表达式。@zipa要在逐字字符串文字中转义双引号,它应该是两倍的。可能的重复项您不只是在寻找
^[^\\\/:*?“|]+$
?假设您想匹配不包含这些字符的文件名。为什么不在C中用
\“
”转义双引号呢,您可以使用一种特殊的方法来检查文件名是否有效。不需要使用正则表达式。@zipa要在逐字字符串文字中转义双引号,它应该是双引号。答案可能重复或合并
var regex=new regex(string.Format(“^[^{0}]$”,regex.escape(new string(Path.GetInvalidFileNameChars())
/
var regex=new regex(string.Format(“[{0}]”),regex.Escape(new string(Path.GetInvalidFileNameChars()))@Milosz:我一直在寻找的完美答案。谢谢。或者组合您的答案
var regex=new regex(string.Format(“^[^{0}]$”),regex.Escape(new string(Path.GetInvalidFileNameChars()))
/
var regex=new regex(string.Format(“[{0}]”),regex.Escape(new string(Path.GetInvalidFileNameChars()))@Milosz:我一直在寻找的完美答案。谢谢。我特别需要正则表达式本身的代码。但这个解决方案也有效。谢谢。我特别需要正则表达式本身的代码。但这个解决方案也有效。谢谢