Need regular expression to extract month,day,time i.e Sep 14 16:08

Need regular expression to extract month,day,time i.e Sep 14 16:08



yes this is C#. The script is reading a txt file containing the line string.



Giving the string below I need to be able to extract the month,day and time such as "Sep 14 16:08":



-rw-r--r-- 1 user001 user001 0 Sep 14 16:08 20180913/labc/0/20180913_02300



I have researched and found the following:


@"(?i)([d]1,2(s)?(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))(s)?[d]1,2");
@"(?i)([d]1,2(s)?(January|Jan|February|feb|March|mar|April|Apr|May|June|July|August|Aug|September|Sep|October|Oct|November|Nov|December|Dec))
(January|Jan|February|feb|March|mar|April|Apr|May|June|July|August|Aug|September|Sep|October|Oct|November|Nov|December|Dec)(s)?[d]1,2");
(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|June)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)s+d1,2



but I can't seem to put together something that will extract "Month Day XX:XX"



Thank you all






Are you looking for the date only in that example string?

– dawg
Sep 18 '18 at 15:41






Better would be to stat the file directly, and use the system structures to get the data rather than parsing the output of ls -l.

– Tanktalus
Sep 18 '18 at 15:43


ls -l






The @"..." pattern strongly suggests this is C# code. If your program is running on a Linux box, @Tanktalus's suggestion is right: Get the file stats directly rather than trying to parse the output of ls. But if your code is running on Windows, and/or you're just consuming text outputted by something outside your control, like a remote script, you'll have to use some kind of regex parsing like this. Can you tell us more about your scenario so we can provide answers closer to your needs?

– Sean Werkema
Sep 18 '18 at 15:47


@"..."


ls






yes this is C#. The script is reading a txt file containing the line string.

– Max
Sep 18 '18 at 16:13






to add to @Tanktalus comment if it's to parse ls output, it's not always consustent, for older dates hour:minutes can be replaced with year, also LANG environment variable should be considered

– Nahuel Fouilleul
Sep 18 '18 at 16:26



LANG




2 Answers
2



I have rewritten you RegEx, so that it matches what you want:


(?:(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))sd1,2sd1,2:d1,2sd4



What it does is first find the month,



then a Space followed by one or two digits



then a Space followed by one or two digits



then a colon followed by one or two digits



then a Space followed by four digits.



You can try it out here






yes I think you got it bud. Thank you!!!!

– Max
Sep 18 '18 at 16:14



You could use ranges to match the days and the time. Note that the match does not validate the date itself.



b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?:3[01]|[12][0-9]|0?[1-9]) (?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9])b


b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?:3[01]|[12][0-9]|0?[1-9]) (?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9])b



That would match


b


(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)


(?:3[01]|[12][0-9]|0?[1-9])


(?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9])


b



Regex demo



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế