C ++ 11 - regex matching
I am extracting information from a string using regex.
auto version = { // comments shows the expected output
// version // output : (year, month, sp#, patch#)
"2012.12", // "2012", "12", "", ""
"2012.12-1", // "2012", "12", "", "1"
"2012.12-SP1", // "2012", "12", "SP1", ""
"2012.12-SP2-1", // "2012", "12", "SP2", "1"
"I-2013.12-2", // "2013", "12", "", "2"
"J-2014.09", // "2014", "09", "", ""
"J-2014.09-SP2-1", // "2014", "09", "SP2", "1"
};
I have the following regex:
// J - 2014 . 09 - SP2 - 1
std::regex regexExpr("[A-Z]?-?([0-9]{4})\\.([0-9]{2})-?(SP[1-9])?-?([1-9])?.*");
and it seems to work well . I'm not very sure about this as I don't have much experience with regex. Is the regex correct and can it be improved?
source to share
You can simply use \w{2,}|\d
as your regex, which matches any combination of word characters of length 2 or more ( \w{2,}
) (to avoid matching j
at the beginning of some lines) or numbers using length 1 ( \d
) (to match 1 at the end of some lines)!
You can use a class template sub_match
for this purpose:
A class template is
sub_match
used by the regular expression engine to denote character sequences matching marked subexpressions. A match is a [begin, end] pair in the target range that matches the regular expression, but with additional observer functions to improve the clarity of the code.
source to share