我需要一个正则表达式来使用RegularExpressionAttribute验证用户名。它应该匹配不包含requestPathInvalidCharacters (<,>,*,%,&,:,\,?) 的任何默认值的用户名。
所以它应该匹配
阿法。
aaf8-a
阿法
它不应该匹配
aa<fa
aaf>a
aaf*a
阿法*
%a
aaf&a
aaf:一个
aaf\a
aaf?a
到目前为止,我一直在调整这个正则表达式,但在其中一种情况下它仍然失败......
^(?!.*(<|>|\*|%|&|:|\\|\?).*)$
[Fact] public void CanValidateAgainstInvalidCharacter() { var result = true; Assert.True(result);
var listWeDontWant = @"<,>,*,%,&,:,\,?";
var inner = listWeDontWant.Split(',').Select(x => Regex.Escape(x)).Aggregate((s, s1) => s + "|" + s1);
//inner = @"\\";
var pattern = @"^(?!.*(" + inner + ").*)$";
Debug.WriteLine(pattern);
//var isMatch = ;
//Debug.WriteLine(isMatch);
pattern = @"^[^<>*%&:\\\?]+$";
Assert.False(Regex.IsMatch("aaf\a", pattern));
// Assert.True(Regex.IsMatch("aafa.", pattern));
// Assert.True(Regex.IsMatch("aaf8-a", pattern));
//Assert.True(Regex.IsMatch("aafa", pattern));
//Assert.True(Regex.IsMatch("aafa", pattern));
//Assert.True(Regex.IsMatch("aa,fa", pattern));
//Assert.True(Regex.IsMatch("aafa", pattern));
//Assert.False(Regex.IsMatch("aa<fa", pattern));
//Assert.False(Regex.IsMatch("aaf>a", pattern));
//Assert.False(Regex.IsMatch("aaf*a", pattern));
//Assert.False(Regex.IsMatch("aafa*", pattern));
//Assert.False(Regex.IsMatch("aaf%a", pattern));
//Assert.False(Regex.IsMatch("aaf&a", pattern));
//Assert.False(Regex.IsMatch("aaf:a", pattern));
//Assert.False(Regex.IsMatch("aaf\a", pattern));
//Assert.False(Regex.IsMatch("aaf?a", pattern));
}