c# - search string for values in specific format -
how can extract values string this: lambertseter_infocloseday_07_09_2014_19_55_52 ?
the format this: <location>_<typeoffile>_<dd>_<mm>_<yyyy>_<hh>_<mm>_<ss>
i tried this:
string mystring = "lambertseter_infocloseday_07_09_2014_19_55_52"; string[] splittedfilestring = mystring.split('_'); string[] formatarray = { "bingohall: ", "filetype: ", "day: ", "month: ", "year: ", "hour: ", "minute: ", "second: " }; (int = 0; < formatarray.length; i++) { formatarray[i] += splittedfilestring[i]; } this seems work @ least, doing right way? there simpler method managing strings way?
if data regular, regular expression work:
var regex = new regex(@"(?<location>\w+)_(?<typeoffile>\w+)_(?<dd>\d+)_(?<mm>\d+)_(?<yyyy>\d+)_(?<hh>\d+)_(?<min>\d+)_(?<ss>\d+)", regexoptions.explicitcapture); var match = regex.match("lambertseter_infocloseday_07_09_2014_19_55_52"); var groups = match.groups; var output = $"bingohall: {groups["location"].value}, filetype: {groups["typeoffile"].value}, day: {groups["dd"].value}, month: {groups["mm"].value}, year: {groups["yyyy"].value}, hour: {groups["hh"].value}, minute: {groups["min"].value}, second: {groups["ss"].value}";
Comments
Post a Comment