Thread: Is there a way to use unescaped backslashes without getting compiler warnings?

  1. #1
    Guest
    Guest

    Is there a way to use unescaped backslashes without getting compiler warnings?

    I'm using regular expressions and having to escape the backslash hurts readability. I assumed raw string literals would free me of this, but it seems GCC doesn't care.
    Code:
    std::regex ohmygod("\\((\\d+),0,'(([^']|\\\\')+)','','([^']|\\\\')*'\\)"); // ok
    
    std::regex unescaped("\((\d+),0,'(([^']|\\')+)','','([^']|\\')*'\))"); // warning
    
    std::regex delimited("R(\((\d+),0,'(([^']|\\')+)','','([^']|\\')*'\))R"); // warning
    Code:
    main.cpp:6:21: warning: unknown escape sequence: '\('
      std::regex delimited("R(\((\d+),0,'(([^']|\\')+)','','([^']|\\')*'\))R");
                         ^
    main.cpp:6:21: warning: unknown escape sequence: '\d'
    main.cpp:6:21: warning: unknown escape sequence: '\)'
    Any way I can avoid this warning?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's because you're using raw string literals wrong. See string literal - cppreference.com.
    Example:
    std::string delimited(R"XX(\((\d+),0,'(([^']|\\')+)','','([^']|\\')*'\))XX");
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Guest
    Guest
    Ah yes, I misinterpreted the (optional). Using R prefix and delimiters together, it works. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Warnings
    By DeanWinchester in forum C Programming
    Replies: 17
    Last Post: 12-25-2011, 10:59 AM
  2. Compiler Warnings
    By Elysia in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2007, 12:44 PM
  3. Compiler warnings
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-22-2003, 01:13 PM
  4. compiler warnings
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 12:06 PM
  5. compiler warnings
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-31-2002, 02:12 PM