I want to extract date from the string through regex.
String : log-bb-2014-02-12-12-06-13-diag
How to do it?
Try this
(\d+)[-.\/](\d+)[-.\/](\d+)
It will match all date formats
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);
}
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 -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With