What should my regular expression look like if I want to validate that $/Folder1/Folder2/Folder3/File.xml always starts with $ and always ends with xml
"$/Folder1/Folder2/Folder3/File.xml"
Pass
"$/Folder1/Folder2/Folder3/File.xm"
Fail
"$/Folder1/Folder2/Folder3/File.py"
Fail
"A/Folder1/Folder2/Folder3/File.xml"
Fail
Edit… So… The right regular expression is…
"^\$.*xml$"
The the method after the implementation of the regex checker looks like…
public bool ValidateConfigPath(string config)
{
var match = Regex.Match(config, @"^\$.*xml$", RegexOptions.IgnoreCase);
return match.Success;
}
And all my unit tests pass…
[TestMethod]
public void ValidateConfigPath_InCorrect1()
{
var t = new TfsWrapper();
var isValid = t.ValidateConfigPath("$/Quantz/Main/CSS Calculator/main.py");
Assert.IsFalse(isValid);
}
[TestMethod]
public void ValidateConfigPath_InCorrect2()
{
var t = new TfsWrapper();
var isValid = t.ValidateConfigPath("C:/Quantz/Main/CSS Calculator/main.xml");
Assert.IsFalse(isValid);
}
[TestMethod]
public void ValidateConfigPath_Correct()
{
var t = new TfsWrapper();
var isValid = t.ValidateConfigPath("$/Quantz/Main/CSS Calculator/main.xml");
Assert.IsTrue(isValid);
}
If there’s not a strict requirement for using regular expressions, I recommend the more straight-forward approach of simply checking the starting and ending characters:
With the above, the intent is absolutely clear to anyone, including people who don’t understand regular expressions.