c++ - c++11 - regex matching -
i extracting info string using regex.
auto version = { // comments shows 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" }; the regex have following:
// j - 2014 . 09 - sp2 - 1 std::regex regexexpr("[a-z]?-?([0-9]{4})\\.([0-9]{2})-?(sp[1-9])?-?([1-9])?.*"); and seems work well. not confident since don't have expertise in regex. regex right , can improved?
you can use \w{2,}|\d regex match combinations of word characters length 2 or more (\w{2,})(to avoid of matching j @ beginning of strings) or digit length 1 (\d)(for match 1 @ end of strings)!
you can use sub_match class template aim:
the class template
sub_matchused regular expression engine denote sequences of characters matched marked sub-expressions. match [begin, end) pair within target range matched regular expression, additional observer functions enhance code clarity.
Comments
Post a Comment