How to check if a regex is a simple string?

Something like this, perhaps:

1
2
3
4
5
6
7
8
bool has_no_unescaped_special_chars( const std::string& pat ) {

    // a regex special character either preceded by any character other than a \ 
    //                           or is at the beginning of the string
    static const std::regex unescaped_special_char_re( R"((?:[^\\]|^)[\^$.*+|?()[\]{}[])" ) ;

    return !std::regex_search( pat, unescaped_special_char_re ) ;
}
a thought: if regex is a bottleneck... check to see if really need it or if some simple code could do the job faster.
You're also copying the string to is_simple_string. If you're dealing with really long strings, this could potentially decrease performance.
Last edited on
Yes, put that & on pat parameter for sure. Good catch. JLBorges fixed it as well.
I know it may not matter. But you have a do-nothing function there. Regex_match does all the work. is-simple string --- does nothing at best, at worst it injects some overhead. If you are short on time and performance, maybe skip that? Or at least see if it is making any difference when you do a billionty of them?

I don't know the regex stuff well performance side. Is search faster than match? Seems like it would be...
Last edited on
Topic archived. No new replies allowed.