Reputation: 154
I have a regular expresion to validate paths of type Number/string/string that looks so: ^[0-9]{1,}(/[0-9a-zA-Z_-]+)*$
If I'm right it should not allow whitespaces, this should be invalid:
2/Pathtest/thirdlevel/bla bla
In online testers like Myregextester it works as expected, but in my c# code it returns true with whitespaces... what am I doing wrong?
if (!Regex.IsMatch(Path.GetDirectoryName(folder.Path).Replace("\\", "/"), @"^[0-9]{1,}(/[0-9a-zA-Z_-]+)*$"))
{
throw new ArgumentException(Resources.Strings.FileNameNotAzureCompatible);
}
Upvotes: 0
Views: 92
Reputation: 175
static void Main(string[] args)
{
var pattern = @"^[0-9]{1,}(/[0-9a-zA-Z_-]+)*$";
string shouldNotMatchString = @"2/Pathtest/thirdlevel/bla bla";
bool shouldBeFalse = Regex.IsMatch(shouldNotMatchString, pattern);
string shouldMatchString = @"2/Pathtest/thirdlevel/blabla";
bool shouldBeTrue = Regex.IsMatch(shouldMatchString, pattern);
}
guess what....shouldBeFale is false, shouldBeTrue is true. your regex seems to be fine.
Upvotes: 1
Reputation: 8359
Your regex seems wrong.
You should escape the /
:
^[0-9]{1,}(\/[0-9a-zA-Z_-]+)*$
Upvotes: 0