Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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# 不允许为Dropbox使用正则表达式的特定字符_C#_Regex - Fatal编程技术网

C# 不允许为Dropbox使用正则表达式的特定字符

C# 不允许为Dropbox使用正则表达式的特定字符,c#,regex,C#,Regex,以下是我必须禁止使用的字符: \ / : ? * < > " | \/:?*" | 以下是我到目前为止的情况: Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); string cleanedFileName = r.Replace(file.FileName,

以下是我必须禁止使用的字符:

\ / : ? * < > " |
\/:?*<>" |
以下是我到目前为止的情况:

Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);

string cleanedFileName = r.Replace(file.FileName, String.Empty);

Regex r=new Regex((?:[^a-z0-9]|)(?看起来您正在尝试清理文件名,如果是,请尝试以下操作:

private static string MakeValidFileName( string name )
{
   string invalidChars = System.Text.RegularExpressions.Regex.Escape( new string( System.IO.Path.GetInvalidFileNameChars() ) );
   string invalidRegStr = string.Format( @"([{0}]*\.+$)|([{0}]+)", invalidChars );

   return System.Text.RegularExpressions.Regex.Replace( name, invalidRegStr, "_" );
}

看起来您正在尝试清理文件名,如果是,请尝试以下操作:

private static string MakeValidFileName( string name )
{
   string invalidChars = System.Text.RegularExpressions.Regex.Escape( new string( System.IO.Path.GetInvalidFileNameChars() ) );
   string invalidRegStr = string.Format( @"([{0}]*\.+$)|([{0}]+)", invalidChars );

   return System.Text.RegularExpressions.Regex.Replace( name, invalidRegStr, "_" );
}

您需要正则表达式吗?您可以简单地将
.Replace()链接起来
适用于所有字符。我不知道,但我打赌这是一种微优化。对于您自己和同事/未来的维护人员来说,这确实更容易理解。为什么要禁止它们?@ErikPhilips这些字符似乎是Windows不允许用于文件名的字符。因为filename。请参阅dropbox。您需要正则表达式吗?您只需ain
.Replace()
适用于所有字符。我不知道,但我敢打赌这是一种微优化。对于您自己和同事/未来的维护人员来说,它确实更容易理解。为什么要禁用它们?@ErikPhilips这些字符似乎是Windows不允许用于文件名的字符。因为文件名。请参阅dropbox。