This is kind of continuation of the previous SO question and its discussion.
Different Between std::regex_match & std::regex_search
In my SO question, the following regex was written to fetch the day from the given input string:
std::string input{ "Mon Nov 25 20:54:36 2013" };
//Day:: Exactly Two Number surrounded by spaces in both side
std::regex r{R"(\s\d{2}\s)"};
In one of the answer, it was changed as R"(.*?\s(\d{2})\s.*)"
to create and hence capture group and first sub-match. Everything works fine for parsing the day information using either regex_match
orregex_search
.
Now I wrote the following regex expressions
to parse various thing from the above input string as follows:
std::string input{ "Mon Nov 25 20:54:36 2013" };
//DayStr:: Exactly Three Letter at the start and followed by spaces(Output: Mon)
std::regex dayStrPattern{ R"(^\w{3}\s)" };
//Day:: Exactly Two Number surrounded by spaces in both side(Output: 25)
std::regex dayPattern{ R"(\s\d{2}\s)" };
//Month:: Exactly Three letter surrounded by spaces in both side(Output: Nov)
std::regex monthPattern{ R"(\s\w{3}\s)" };
//Year:: Exactly Four Number at the end of the string(Output: 2013)
std::regex yearPattern{ R"(\s\d{4}$)" };
//Hour:: Exactly two Number surrounded by spaces in left side and : in right side(Output:20)
std::regex hourPattern{ R"(\s\d{2}:{1})" };
//Min:: Exactly two Number sorruounded by : in left side and : in right side(Output: 54)
std::regex minPattern{ R"(:{1}\d{2}:{1})" };
//Second::Exactly two Number surrounded by : in the left side and space in right side(Output: 36)
std::regex secPattern{ R"(:{1}\d{2}\s)" };
I have tested the above regex here and they seems to be correct.
Now can we use the grouping mechanism here so that we pass a single regex expression in the method std::regex_search
instead of 7 different regex.?. This way std::regex_search would store the output into its std::smatch
sub-match vector. Is it possible over here?. I read documentation
and A Tour Of C++ book
but did not get understand much about regular expression grouping
.
In general when and how we should use/design grouping so that we get various information in one call of std::regex_search?
At this point I have to call 7 times std::regex_search with different regex expression to fetch various information and then use it. I certainty think there is better way to achieve it than what i am doing right now.