Created
December 5, 2017 01:04
-
-
Save JNeiger/5f7798655dbee4f370ceb2eca4564f6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <regex> | |
#include <iostream> | |
#include <algorithm> | |
int main(int argc, char *argv[]) { | |
std::string input = "5mm asdf 6mm asdf"; | |
std::string output = ""; | |
// If we get a good string from user, use that | |
if (argc == 2) { | |
input = argv[1]; | |
} | |
// Matches words that have a digit, zero or more whitespaces, then the mm | |
std::regex reg("[[:digit:]]+[[:space:]]*mm"); | |
std::smatch matches; | |
// Match regex to string and record all the matches | |
while (std::regex_search(input, matches, reg)) { | |
// For each match | |
// Pull everything before the match | |
std::string pre = matches.prefix().str(); | |
// Make it caps | |
std::transform(pre.begin(), pre.end(), pre.begin(), ::toupper); | |
// Remove spaces from match | |
std::string match = matches[0].str(); | |
match.erase(remove_if(match.begin(), match.end(), isspace), match.end()); | |
// Add to output variable | |
output += pre + match; | |
// Continue eating the string until all matches are found | |
input = matches.suffix().str(); | |
} | |
// Add anything after the last match to the output | |
std::transform(input.begin(), input.end(), input.begin(), ::toupper); | |
output += input; | |
// Print out results | |
std::cout << output << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment