Conditionally ignore case in c++11 regular expressions

I'm trying to write a function which will allow the user to specify if they want to ignore case in a regular expression match. I've come up with a solution, but it's pretty clunky. Is there a way to conditionally set the std::regex_constants::icase flag when building the regex?

#include <string>
#include <regex>

std::string sub(std::string string, std::string match, bool ic){
  std::regex r;
  std::regex rc(match, std::regex_constants::collate);
  std::regex ric(match, std::regex_constants::icase | std::regex_constants::collate);
  if(ic){
    r = ric;
  } else {
    r = rc;
  }
  std::smatch matches;
  if(std::regex_search(string,matches, r)){
    return matches[0];
  } else {
    return "no match";
  }
}

2 answers

  • answered 2018-01-11 21:13 Deduplicator

    There are many ways to conditionally set the flag. For example using the conditional operator:

    std::regex r(match, ic ? std::regex_constants::icase | std::regex_constants::collate
        : std::regex_constants::collate);
    

  • answered 2018-01-11 22:04 zett42

    In such cases I prefer good old if for readability:

    auto flags = std::regex_constants::collate;
    if(ic) flags |= std::regex_constants::icase;
    std::regex r(match, flags);
    

    This code will also be easier to maintain than a version with conditional operator ?. Consider that you want to add another conditional flag in the future. It's a simple as adding another if line:

    if(ns) flags |= std::regex_constants::nosubs;
    

    Try to do this with conditional operator and the code will quickly degrade into spaghetti.