16

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++.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
RDismyname
  • 173
  • 1
  • 1
  • 10
  • 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. – sashang Oct 16 '12 at 06:14
  • But i thought regex can store the matches in a match_results object? – RDismyname Oct 16 '12 at 06:27
  • sashang -- please don't post misinformation like this. If you don't know, don't post. – Stephen F. Heffner Sep 10 '21 at 22:10

2 Answers2

25

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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 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 Oct 16 '12 at 06:22
  • @RDismyname Added example program to my answer. – Some programmer dude Oct 16 '12 at 07:22
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.

kuperspb
  • 339
  • 2
  • 10
  • 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? – RDismyname Oct 16 '12 at 06:41
  • Good article for start using tr1 regex. http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339/A-TR1-Tutorial-Regular-Expressions.htm – kuperspb Oct 16 '12 at 06:44