Regex to check if the file format is as expected in C# -
my input list of filenames complete path , need extract items filenames strictly suit below filename format.
generic filename format.
**c:\my\path\to\file\filename_yyyy-mm-dd_hh-mm-ss.ext**
i have tried following regex pattern still see noise.
string regexpattern = @"filename_[2-9][0-9]{3}-[0-1][0-9]-[0-3][0-9]_[0-2][0-9]-[0-5][0-9]-[0-5][0-9]\.ext$"
let me know if missing something.
why use regex when can use datetime
s built in parser this:
string input = c:\my\path\to\file\filename_yyyy-mm-dd_hh-mm-ss.ext; string filename = path.getfilenamewithoutextension(input); string[] parts = filename.split('_'); if (parts.length != 3) { /*invalid*/ } if (path.getextension(input) != "ext") { /*invalid*/ } if (parts[0] != "filename") { /*invalid*/ } datetime dt; if (!datetime.tryparseexact(parts[1] + "_" + parts[2], "yyyy-mm-dd_hh-mm-ss", cultureinfo.invariantculture, datetimestyles.none, out dt)) { /*invalid*/ } //isvalid
Comments
Post a Comment