Skip to content

Instantly share code, notes, and snippets.

@JNeiger
Created December 5, 2017 01:04
Show Gist options
  • Save JNeiger/5f7798655dbee4f370ceb2eca4564f6c to your computer and use it in GitHub Desktop.
Save JNeiger/5f7798655dbee4f370ceb2eca4564f6c to your computer and use it in GitHub Desktop.
#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