0

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!):

^(\"(?:[^\"]|\\\")*\"|\-?[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?

  • 1
    Many regexp engines have \Q ... \E for exactly this purpose. Look at the documentation of the one you're using. – Kilian Foth May 25 '16 at 11:54
  • Call an escaping function compatible with the language you're using? Or use a raw-string-literal feature if your language offers it. – CodesInChaos May 25 '16 at 11:54
  • @KilianFoth I think this is about the language in which you embed the regex as a string literal requiring further escapes, not about the escape sequences regex needs. – CodesInChaos May 25 '16 at 11:56
  • 4
    Many IDEs (e.g. Eclipse) have a setting "add escapes when pasting into literals" that does exactly this. Just write "" and then paste your regex into that. – Kilian Foth May 25 '16 at 12:04
  • 4
    regex101.com has a "code generator" button, which generates PHP, Javascript and Python and escapes the regex. There's no C++ or Java but you could probably just copy the regex from the PHP string. – kapex May 25 '16 at 16:43
0

Use Unicode character escapes instead of literals. For example:

  • Java

    boolean b = Pattern.matches("\u0022", '"');
    
  • JavaScript

    /\u0022/.test('"');
    
  • Perl

    '"' =~ /\N{U+0022}/;
    

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

  • Java

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
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

  • While this does solve the problem at hand, it leaves behind the problem that it makes the regular expression even more cryptic than it was to start with. Of course, you already have two problems anyway, so maybe this third isn't a huge issue... – Jules Aug 18 '18 at 10:59
0

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.

0

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

// ^(\"(?:[^\"]|\\\")*\"|\-?[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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.