about string and regex


why is that when define regex_string there are three slashes \\\ infront the dot , however when i define str i only need two slashes \\ , would i not have to escape the dot "." when i define a regular string, so should i not use three slashes instead ?

also why don't i have escape the opening and closing parenthesis "(" and ")" in the regular string ?



1
2
3
4
5
6
7
8
9
10
11
12
13
14


	std::string str("\"\\.(cpp)\"");

	const std::string regex_string = R"!(\"\\\.\([[:alpha:][:digit:]]*\)\")!";
	const std::regex ext(regex_string);

	if (regex_match(str, ext))
	{
		cout << "regex" << endl; 
	}


could someone enlighten me on this ??
Quote from a previous thread:

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

I'm not sure, are you asking about the parentheses used in the string constructor?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main() 
{
    std::string str("\"\\.(cpp)\"");

    std::cout << "contents of str = " << str << "\n";   
    
    
    
    std::string s2 = "\"\\.(cpp)\"";

    std::cout << "contents of s2 =  " << s2 << "\n";       
}
contents of str = "\.(cpp)"
contents of s2 =  "\.(cpp)"

Last edited on
std::string str("\"\\.(cpp)\""); ---> two backslashes \\ in front the dot "."



const std::string regex_string = R"!(\"\\\.\([[:alpha:][:digit:]]*\)\")!"; ---> three slashes \\\ in front of the dot "."



std::string str("\"\\.(cpp)\""); ----> opening and closing parenthesis "(" and ")" are not escaped



const std::string regex_string = R"!(\"\\\.\([[:alpha:][:digit:]]*\)\")!"; ---> opening and closing parenthesis "(" and ")" are escaped


i am comparing the regular string with the construction of the raw string
Last edited on
I think you need to distinguish between three separate concepts.

1. Which characters have a special meaning when used in a C++ string.

2. Which characters have a special meaning when used in a regular expression.

3. In the context of C++strings, the difference between traditional representation of a literal, and the raw-string approach to representing a literal.

Topic archived. No new replies allowed.