> so is it better to escape the "." and why ?
In a regular expression, the period '.' is a special character (a metacharacter).
To use it literally, it needs to be escaped.
.cpp - any character, followed by cpp (matches acpp, bcpp, xcpp, .cpp, ycpp)
\.cpp - the period, followed by cpp (matches .cpp)
> with a double slash \\
In C++ string literals too, the backslash is treated specially as an escape character.
To specify a single literal backslash, we would have to write "\\"
So to specify the regular expression
\.cpp we would write
const std::regex re( "\\.cpp" )
When many backslashes are involved, using raw string literals would be easier:
std::regex re( R"(\.cpp)" )
http://www.stroustrup.com/C++11FAQ.html#raw-strings
> why is the dollar sign "$" only applied for hpp
To make it applicable to all of the alternatives, place the $ outside the closing parenthesis for the alternatives.
1 2
|
// a. literal '.' b. one of cpp|c|h|hpp c. end of line/string
regex extensions( "\\.(cpp|c|h|hpp)$" );
|