C# Check string for specific length of numbers -
i have ability search , return files in given file location. have ability return number sequence file name such:
public list<availablefile> getavailablefiles(string rootfolder) { list<availablefile> files = new list<availablefile>(); if (directory.exists(rootfolder)) { log.info("checking folder: " + rootfolder + " files"); try { foreach (string f in directory.getfiles(rootfolder)) { files = fileupload.createfilelist(f); var getnumbers = new string(f.where(char.isdigit).toarray()); system.diagnostics.debug.writeline(getnumbers); } } catch (system.exception excpt) { log.fatal("getavailablefiles failed: " + excpt.message); } } return files; }
what want return sequence of numbers 8 characters long. example file name new file1 12345678 123
i'm caring getting 12345678
back.
how can modify method achieve this?
a regex seems this:
var r = new regex(".*(\\d{8})"); foreach (string f in directory.getfiles(rootfolder)) { files = fileupload.createfilelist(f); var match = r.match(f); if(m.success) { console.writeline(m.groups[1]); // aware index 0 contains entire matched string } }
the regex match first occurence of 8 digits , put groupscollection
.
Comments
Post a Comment