Regular expression to filter out time and date

Hi there,

I need to separate day, month, year, hour, min from a string in the following format.  How can I do this using Regular Expressions.  Thank you.
18.06.2015 22:17
ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
'sscanf()' (http://www.cplusplus.com/reference/cstdio/sscanf/?kw=sscanf) might be easier than a regular expression here, e.g.

#include <stdio.h>

// ...
char sample_date[] = "18.06.2015 22:17";
int day, mon, year, hour, min;

sscanf(sample_date, "%d.%d.%d %d:%d", &day, &mon, &year, &hour, &min);

Open in new window

0
ambuliAuthor Commented:
Thanks, but how about using C++?
0
ambuliAuthor Commented:
I came up with this one.  Not sure if there is any issues.  Seems to work for now.

#include <boost/regex.hpp>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    boost::regex pattern("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2})");
    boost::match_results<std::string::const_iterator> matches;
    if(0 != boost::regex_match(string("1994-10-21 14:41"), matches, pattern))
    {
        cout << "pattern matches...." << endl;
        string value1(matches[1].first, matches[1].second);
        string value2(matches[2].first, matches[2].second);
        string value3(matches[3].first, matches[3].second);
        string value4(matches[4].first, matches[4].second);
        string value5(matches[5].first, matches[5].second);

        cout << value1 << ", " << value2 << "," << value3 << "," << value4 << "," << value5 << endl;

    }
    else
    {
        cout << "No matching string found" << endl;
    }

    //cout << MSG << endl;

    return 0;
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jkrCommented:
Don't want to be nitpicking, but that seems like using a sledgehammer to crack a nut... I mean two lines 'sscanf()' using standard libraries vs. 25 lines and using boost ;o)

Nothing against boost, if you are already using it in your project, this does not make a big difference. but if you'd had to add it just for this task, I wouldn't do it.
0
sarabandeCommented:
I need to separate day, month, year, hour, min from a string
in c++ you may use stringstream for that task:

// assuming strtimestamp is like "31-12-2014 17:16"
std::istringstream iss(strtimestamp);
int day, month, year, hour, minute;
char c;
if (!(iss >> day >> c >> month >> c >> year >> c >> hour >> c >> minute))
{
    std::cout << "invalid timestamp" << std::endl;
    return -1;
}

Open in new window


Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Thank you all.  Yes, I was doing something in the same class with boost so I thought of doing that way.  Yes, it is unnecessary .
0
sarabandeCommented:
Yes, it is unnecessary .
of course not. boost regular expressions is a mighty tool and it is of course worth to make some experiences with it. the c solution jkr has posted and the c++ way i provided only can show that there are more than one way to achieve a goal. if you try all alternatives you finally can find out what is the best (for you).

Sara
0
ambuliAuthor Commented:
Thank you Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.