I need to format a string that contains a MAC address (for logging). The string is of the format "xxxxxxxxxxxx" and I need to standardize it to "xx-xx-xx-xx-xx-xx". Whats a good regex for this purpose?
I'm compiling C++ code using GCC 4.4.7. Also, I'd prefer to use boost regex. However, if there are any simpler alternatives please let me know. This is what I have so far
std::string s = "e8f5a4b3e8e4";
boost::regex expr("^ *([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2}) *$", boost::regex::extended | boost::regex::icase);
std::string fmt{"\\1-\\2-\\3-\\4-\\5-\\6"};
std::cout << boost::regex_replace(s, expr, fmt) << '\n';
I'd like to know if there's an optimized version of the regex that I'm using.
I've tried this regex - ^ *([[:xdigit:]]{2}){6} *$
at https://regex101.com but it doesn't capture all the groups. It captures only the last one.