0

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.

4
  • How can you have a slash in a file name? Commented Nov 18, 2018 at 11:08
  • @KlausGütter it appears to me the file name is 18.zip, and /2018/ + /11/ are simply subfolders Commented Nov 18, 2018 at 11:10
  • @K.Dᴀᴠɪs: ahh, yes, makes sense Commented Nov 18, 2018 at 11:11
  • I am sorry I was not accurate. Please ignore that I wrote file name and consider it a string that I need to extract the date from. Thanx Commented Nov 18, 2018 at 11:12

3 Answers 3

2
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

One-liner:

var date = DateTime.Parse(Regex.Match(@"e1e2e854-e1cd-4980-83bb-2a0273f904aa/2018/11/18.zip", @"/(\d+/\d+/\d+)").Groups[1].Value);
Sign up to request clarification or add additional context in comments.

12 Comments

I am trying to do something like: DateTime dateTime = DateTime.ParseExact(name, format, formatprovider). I would appreciate if you could provide me with the format here :)
@AkramShahda Well, my first code (see in edited) extracts year, month and time separately. You can do with them whatever you want. :)
Thank you but what I really need is a one liner code to get there.
@AkramShahda Added one-liner 😉
As the filename format is fixed, using three capture groups wouldn't be better than capture all in one and then having to parse the date again?
|
2

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);

2 Comments

I am trying to do something like: DateTime dateTime = DateTime.ParseExact(name, format, formatprovider). I would appreciate if you could provide me with the format here :)
Why not using "/(\\d+)/(\\d+)/(\\d+)" and then use the three captured groups separately to build a date with the native con. Why to parse the date again to separate the three fields?
0

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];

enter image description here

and you can parse your parts like;

var myDate = DateTime.ParseExact($"{year}{day}{month}",
                                  "yyyyddMM",
                                  CultureInfo.InvariantCulture);

3 Comments

Thank you but don't you think it is too much code for one simple purpose?
@AkramShahda You don't need regex for that. Use just simple string manipulations. It is not an expensive operation after all. This is just 5 lines of code. Is it really too much? Don't you think it is more readable than regex one? Your choice..
@SonerGönül, why don't use regex for that? Do you think that using simple string manipulations (each requiring one pass through the data, at least) will be better than doing all in just one pass? Do you actually think that using .split() three times on the same String will be more efficient than getting the three fields in one shot? Do you know that there are more things than using .split() ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.