Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping String constants for embedding in a std::regex

Tags:

C++11 has 6 different regular expression grammars you can use. In my case, I am interacting with a component that is using modified ECMAScript regular expressions.

I need to create a regular expression "match a string starting with X", where X is a string literal I have.

So the regular expression I want is roughly ^X.*. Except the string X could contain more regular expression special characters, and I want them to occur.

Which means I really want ^ escaped(X) .*.

Now, I can read over the ECMAScript documentation, find all of the characters which have a special meaning, write a function that escapes them, and be done. But this seems inelegant, inefficient, and error prone -- especially if I want to support all 6 kinds of regular expressions that C++ supports currently, let alone in the future.

Is there a simple way in the standard to escape a literal string to embed in a C++ regular expression, possibly as a function of the regular expression grammar, or do I have to roll my own?

Here is a similar question using the boost library, where the list of escapes is hard-coded, and then a regular expression is generated that backslashes them. Am I reduced to adapting that answer for use in std?