'\' is an escape character in C++ and in regex... so when you type "\\" in code, it's actually a single \ character.
So your str string contains a single slash character, and your regex string is also a single slash character. But since regex uses slashes to escape certain codes, a single slash character doesn't mean anything.
So you need to escape both of them:
1 2 3
regex ex("\\\\"); // <- C++ escapes \, so this is actually "\\", which regex will then escape to '\'
string str="\\"; // <- C++ escapes to '\'
...
EDIT:
One of the things lacking in C++ is the ability for "raw" strings where characters don't have to be escaped. Working with regex is actually a big pain without raw strings.
Thank you very much, I am still new to regex expressions. I thought I had already escaped the special charachters with regex. I can now see the benefit of having raw strings.
> One of the things lacking in C++ is the ability for "raw" strings where characters don't have to be escaped.
> Working with regex is actually a big pain without raw strings.