The following code does not work all the time. I have looked at countless regex examples but very few address the use of multiple extensions.
public bool FAQPNFileCheck(string name)
{
if (name.Length > 0)
{
Match match = Regex.Match(name,
@"\\([A-Za-z0-9_-]+)\.(jpg|doc|pdf)$",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
return true;
//Console.WriteLine(key);
}
}
if (name == "")
{
return true;
}
return false;
}
Ok, so after all, you want to allow files with the extesions jpg, doc or pdf, right?
Let’s try this:
As latkin pointed out, if you’re going to use this
Regexobject once, thenRegexOptions.Compiledis not a good choice, because it will take longer to instantiate the object. However, the match will run faster, so it’s a good idea to keep it if you’re going to use it on several files (as I was supposing), then keep it at aRegexinstance.