The code is in C#. If I use in regex simple string the code is working (for example fileName="Test"), but if I use special characters(- ( ) [ ] {} ! . , ` ~ @ # % ; = - + &) appear problems.
fileName = "Test- ( ) [ ] {} ! . , ` ~ @ # % ; = - + &";
string pattern = ".*" + fileName + @"_\d{2}_\d{2}_\d{2}.xml";
//pattert = ".*" + "Test- ( ) [ ] {} ! . , ` ~ @ # % ; = - + &" + @"_\d{2}_\d{2}_\d{2}.xml";
Regex rgx = new Regex(pattern);
if (rgx.IsMatch("..\\"+"Test- ( ) [ ] {} ! . , ` ~ @ # % ; = - + &_13_45_23.xml"))
{
}
There are chances to use these special characters. How can I resolve this problem?
Pravesh Singh
06-Mar-2014You should use the Regex.Escape method if you want to safely handle special characters in fileName, for example: