Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a regex search in C++

Tags:

c++

regex

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

like image 731
RDismyname Avatar asked Oct 16 '12 06:10

RDismyname


People also ask

Can you use regex in C?

A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.

What does regex search Return C++?

It returns whether some sub-sequence in the target sequence (the subject) matches the regular expression rgx (the pattern). The target sequence is either s or the character sequence between first and last, depending on the version used.

How do you find in regex?

Regular expressions are a flexible and powerful notation for finding patterns of text. To use regular expressions, open either the Find pane or the Replace pane and select the Use check box. When you next click Find Next, the search string is evaluated as a regular expression.

How do regex searches work?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


2 Answers

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
like image 147
Some programmer dude Avatar answered Sep 29 '22 11:09

Some programmer dude


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.

like image 38
kuperspb Avatar answered Sep 29 '22 09:09

kuperspb