How to escape regexes and paths in double quote strings without going crazy?

It often happens that after designing my regexp (on regex101.com) I want to paste it in my program. Consider this regexp that matches numbers and string (but keep in mind this is general question!):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(<del>"</del>(?:[^<del>"</del>]|<del>\"</del>)*<del>"</del>|<del></del>-?[0-9]+(?:<del></del>.[0-9]+)?)$
^(<del>"</del>(?:[^<del>"</del>]|<del>\"</del>)*<del>"</del>|<del></del>-?[0-9]+(?:<del></del>.[0-9]+)?)$
^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$

I overlined all characters that need to be escaped before pasting them into languages that use " for strings.

Needless to say, doing this manually drives me crazy. I face this problem both at work with C++ project and at home with Java and JavaScript projects.

How can I deal with this efficiently?

10

If you feel it to be worth make your small DSL (or maybe it already exists) so you can do (java):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// ^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$
// @formatter:off
Pattern pattern = Patterning.start() // ^
.group()
.lookahead()
...
.set("0-9").plus()
.string("E=m.c^2") // Q ... E
.lookaheadEnd()
.groupEnd();
.end() // $
.build();
// @formatter:on
class Patterning { ... }
</code>
<code>// ^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$ // @formatter:off Pattern pattern = Patterning.start() // ^ .group() .lookahead() ... .set("0-9").plus() .string("E=m.c^2") // Q ... E .lookaheadEnd() .groupEnd(); .end() // $ .build(); // @formatter:on class Patterning { ... } </code>
// ^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$
// @formatter:off
Pattern pattern = Patterning.start() // ^
    .group()
    .lookahead()
        ...
        .set("0-9").plus()
        .string("E=m.c^2") // Q ... E
    .lookaheadEnd()
    .groupEnd();
    .end()                               // $
    .build();
// @formatter:on

class Patterning { ... }

Though most people know regex; or at least it is worth learning regex, if only to do powerfull replaces in the editor.

1

In C++, use raw string literals (added in C++11). Nothing between the delimiter sequences is treated as an escape:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const char *regex = R"-regexp-(^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$)-regexp-";
</code>
<code>const char *regex = R"-regexp-(^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$)-regexp-"; </code>
const char *regex = R"-regexp-(^("(?:[^"]|\")*"|-?[0-9]+(?:.[0-9]+)?)$)-regexp-";

in this case the delimiters are the literal strings -regexp-( and )-regexp-

1

Use Unicode character escapes instead of literals. For example:

  • Java

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>boolean b = Pattern.matches("u0022", '"');
    </code>
    <code>boolean b = Pattern.matches("u0022", '"'); </code>
    boolean b = Pattern.matches("u0022", '"');
    
  • JavaScript

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>/u0022/.test('"');
    </code>
    <code>/u0022/.test('"'); </code>
    /u0022/.test('"');
    
  • Perl

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>'"' =~ /N{U+0022}/;
    </code>
    <code>'"' =~ /N{U+0022}/; </code>
    '"' =~ /N{U+0022}/;
    

In addition, strings that are compiled to regular expressions can use line breaks for added clarity:

  • Java

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>boolean phone_mask = Pattern.matches("^[^0-9]*"/* Optional non-numeric characters */ +
    "\+9{3}" /* Followed by a plus sign and three nines */ +
    "\s9" /* Followed by a space and one nine */ +
    "\s9{3}" /* Followed by a space and three nines */ +
    "\s9{4}" /* Followed by a space and four nines */ +
    "$", "Phone: +999 9 999 9999");
    </code>
    <code>boolean phone_mask = Pattern.matches("^[^0-9]*"/* Optional non-numeric characters */ + "\+9{3}" /* Followed by a plus sign and three nines */ + "\s9" /* Followed by a space and one nine */ + "\s9{3}" /* Followed by a space and three nines */ + "\s9{4}" /* Followed by a space and four nines */ + "$", "Phone: +999 9 999 9999"); </code>
    boolean phone_mask = Pattern.matches("^[^0-9]*"/* Optional non-numeric characters */ +
                            "\+9{3}" /* Followed by a plus sign and three nines */ +
                            "\s9"    /* Followed by a space and one nine */  +
                            "\s9{3}" /* Followed by a space and three nines */ +
                            "\s9{4}" /* Followed by a space and four nines */ +
                            "$", "Phone: +999 9 999 9999");
    
  • JavaScript

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>var phone_mask = RegExp("^[^0-9]*"/* Optional non-numeric characters */ +
    "\+9{3}" /* Followed by a plus sign and three nines */ +
    "\s9" /* Followed by a space and one nine */ +
    "\s9{3}" /* Followed by a space and three nines */ +
    "\s9{4}" /* Followed by a space and four nines */ +
    "$").test("Phone: +999 9 999 9999");
    </code>
    <code>var phone_mask = RegExp("^[^0-9]*"/* Optional non-numeric characters */ + "\+9{3}" /* Followed by a plus sign and three nines */ + "\s9" /* Followed by a space and one nine */ + "\s9{3}" /* Followed by a space and three nines */ + "\s9{4}" /* Followed by a space and four nines */ + "$").test("Phone: +999 9 999 9999"); </code>
    var phone_mask = RegExp("^[^0-9]*"/* Optional non-numeric characters */ +
                            "\+9{3}" /* Followed by a plus sign and three nines */ +
                            "\s9"    /* Followed by a space and one nine */  +
                            "\s9{3}" /* Followed by a space and three nines */ +
                            "\s9{4}" /* Followed by a space and four nines */ +
                            "$").test("Phone: +999 9 999 9999");
    

References

  • Character Escapes in Regular Expressions | Microsoft Docs
  • Java Regex Examples Matching Characters
  • perlrequick – perldoc.perl.org
  • Java literals
  • pcreunicode specification
  • Maintaining Regular Expressions

1

Write a program that interprets and escapes your regexps for you.
You can either use this to generate the code needed to paste into your source or have it work on the fly having your regexp in a separate file.

For the file version, a big downside is: not having your logic with your source.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật