17

Hello I am new to regular expressions and from what I understood from the c++ reference website it is possible to get match results.

My question is: how do I retrieve these results? What is the difference between smatch and cmatch? For example, I have a string consisting of date and time and this is the regular expression I wrote:

"(1[0-2]|0?[1-9])([:][0-5][0-9])?(am|pm)"

Now when I do a regex_search with the string and the above expression, I can find whether there is a time in the string or not. But I want to store that time in a structure so I can separate hours and minutes. I am using Visual studio 2010 c++.

3
  • 1
    all the regex finding will do is tell you where in the string a substring exists that matches the regex expression; in your case the substring is a date time. You then have to write some code to parse that substring and lift the data from it into a struct. regex isn't going to magically do that for you. Commented Oct 16, 2012 at 6:14
  • But i thought regex can store the matches in a match_results object? Commented Oct 16, 2012 at 6:27
  • sashang -- please don't post misinformation like this. If you don't know, don't post. Commented Sep 10, 2021 at 22:10

2 Answers 2

26

If you use e.g. std::regex_search then it fills in a std::match_result where you can use the operator[] to get the matched strings.

Edit: Example program:

#include <iostream>
#include <string>
#include <regex>

void test_regex_search(const std::string& input)
{
    std::regex rgx("((1[0-2])|(0?[1-9])):([0-5][0-9])((am)|(pm))");
    std::smatch match;

    if (std::regex_search(input.begin(), input.end(), match, rgx))
    {
        std::cout << "Match\n";

        //for (auto m : match)
        //  std::cout << "  submatch " << m << '\n';

        std::cout << "match[1] = " << match[1] << '\n';
        std::cout << "match[4] = " << match[4] << '\n';
        std::cout << "match[5] = " << match[5] << '\n';
    }
    else
        std::cout << "No match\n";
}

int main()
{
    const std::string time1 = "9:45pm";
    const std::string time2 = "11:53am";

    test_regex_search(time1);
    test_regex_search(time2);
}

Output from the program:

Match
match[1] = 9
match[4] = 45
match[5] = pm
Match
match[1] = 11
match[4] = 53
match[5] = am
Sign up to request clarification or add additional context in comments.

2 Comments

Yes Joachim but how do i know the location of the matched string in the smatch object i create? for example in the string "from 10am to 12pm" how do i store "10am" in a variable and "12pm" in a different variable?
@RDismyname Added example program to my answer.
1

Just use named groups.

(?<hour>(1[0-2]|0?[1-9]))([:](?<minute>[0-5][0-9]))?(am|pm)

Ok, vs2010 doesn't support named groups. You already using unnamed capture groups. Go through them.

2 Comments

sorry i dont understand your answer. I am new to regex and basically what i want to do for example is that with an input string "from 10 am to 12 pm" i want to pull 10am into a string variable and 12 pm into another variable. I can use regex search to find whether the string has a time but how do i extract that time?
Good article for start using tr1 regex. codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339/…

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.