I have the following structure for names:
GuidAsString/yyyy/MM/dd.zip
An example:
e1e2e854-e1cd-4980-83bb-2a0273f904aa/2018/11/18.zip
I would like to extract the date using regex. Regex is just not my thing.
Update: I am trying to do something like:
DateTime dateTime = DateTime.ParseExact(name, format, formatprovider)
and I am looking for the format. If you have a more elegant way that would be even better.
I would appreciate your help.
string input = @"e1e2e854-e1cd-4980-83bb-2a0273f904aa/2018/11/18.zip";
string pattern = @"/(\d+/\d+/\d+)";
var match = Regex.Match(input, pattern);
var date = DateTime.Parse(match.Groups[1].Value);
WriteLine(date.ToShortDateString());
// Prints: 18.11.2018
var date = DateTime.Parse(Regex.Match(@"e1e2e854-e1cd-4980-83bb-2a0273f904aa/2018/11/18.zip", @"/(\d+/\d+/\d+)").Groups[1].Value);
you could use to extract the date in string format with regex:
var st = "e1e2e854-e1cd-4980-83bb-2a0273f904aa/2018/11/18.zip";
var st1 = Regex.Match(st,@"\d{4}/\d{2}/\d{2}").Value;
st1 = "2018/11/18" if you want to convert in date format use:
var date = DateTime.Parse(st1);
You easily find the solution with some string manipulation. You can use String.Split
method like;
var str = "e1e2e854-e1cd-4980-83bb-2a0273f904aa/2018/11/18.zip";
var year = str.Split('/')[1];
var month = str.Split('/')[2];
var day = str.Split('/')[3].Split('.')[0];
and you can parse your parts like;
var myDate = DateTime.ParseExact($"{year}{day}{month}",
"yyyyddMM",
CultureInfo.InvariantCulture);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.