0

Disclaimer: I have only recently started with C++ and I am still trying to understand its basic concepts.

Say, for example, we have to following statement:

std::cout << "^\\s+Encryption key:(\\w+)"        << '\n' << 
             "^\\s+Quality=(\\d+)"               << '\n' <<
             "^\\s+E?SSID:\"([[:print:]]+)\""    << '\n' <<
             "^\\s+ssid=\"([[:print:]]+)\""      << '\n';

How can one convert this to a single raw string literal? I tried to read as much as possible about this subject, but can't seem to figure out how to do this. Could anyone point me in the right direction?

I understand )" can not be used directly in a RSL.

3
  • There: "^\\s+Encryption key:(\\w+)\n^\\s+Quality=(\\d+)\n^\\s+E?SSID:\"([[:print:]]+)\"\n^\\s+ssid=\"([[:print:]]+)\"\n". It is a single raw string literal. Commented Sep 14, 2018 at 20:12
  • A raw string literal is something different. eel.is/c++draft/lex.string#def:raw_string_literal Commented Sep 14, 2018 at 20:19
  • @Swordfish I saw your comment and removed my answer as it was wrong. Thank you for the clarification. Commented Sep 14, 2018 at 20:23

1 Answer 1

5

You can use ( and ) in raw string literals. You just have to specify the start/stop delimiter sequence so that you don't get a delimiter sequence preceded by a ) in the string. The null character is always appended. See cppreference string literal documentation for details.

In the following example, ### is the delimiter, but you can select whatever you want (except these characters are not allowed: () \, and the length must be 16 characters or fewer). Just select something that won't cause you to accidentally terminate the literal prematurely.

With ### as the delimiter, the compiler will continue consuming text as part of the raw string literal until it comes to the combination )###", so technically you could have )### in your text without terminating the raw string literal, but I think it's best to play it safe -- you could inadvertently terminate the literal prematurely if there happens to be a " immediately after that sequence. I'd also avoid the situation where you have the delimiter followed by ( in the body of the literal -- ###( in this example. It's allowed, but could confuse people reading the program.

So this could be

std::cout << R"###(^\s+Encryption key:(\w+)
^\s+Quality=(\d+)
^\s+E?SSID:"([[:print:]]+)"
^\s+ssid="([[:print:]]+)"
)###"  ;

In a raw string literal, you don't escape characters, so \\ becomes \ and \" becomes " in your example, and \n become actual newlines.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.