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 datetimes 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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -