Reputation: 341
I want to extract date from the string through regex.
String : log-bb-2014-02-12-12-06-13-diag
How to do it?
Upvotes: 10
Views: 39492
Reputation: 153
I have checked all above suggestions but none worked for if date format is "November 11, 2000". As this was a constant format that I need to extract for my problem here is the solution for it.
[A-Z]\w*\s\d*\W*\d{4}
Note: This regex is specifically to extract date in the above mentioned format, if the order is randomized for eg. "11 November, 2000". This Regex won't give suitable result. However slight modification in the above re, would give proper results.
Upvotes: 0
Reputation: 28403
Try this
(\d+)[-.\/](\d+)[-.\/](\d+)
It will match all date formats
Upvotes: 11
Reputation: 11116
Considering your date to be just 2014-02-12
i.e. 12th feb 2014.I hev written the below code to extract that part using ruby
str = 'log-bb-2014-02-12-12-06-13-diag'
str.scan(/\d{4}.\d{2}.\d{2}/)
will return ["2014-02-12"]
regex is written within two /
\d
means any integer,
{}
curly braces with any integer means number of times it has been repeated, .
means any character. Here I have used it for -
Upvotes: 3
Reputation: 15941
Here something to start:
string s = "log-bb-2014-02-12-12-06-13-diag";
Regex r = new Regex(@"\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}");
Match m = r.Match(s);
if(m.Success)
{
DateTime dt = DateTime.ParseExact(m.Value, "yyyy-MM-dd-hh-mm-ss", CultureInfo.InvariantCulture);
}
Upvotes: 13