Search code examples
regexstringracketrawstring

Racket raw strings


In Racket you have to escape backslashes in strings, therefore Windows paths and regexes become verbose.

For example, the regular expression (.*)\1 can be represented with the string "(.*)\\1" or the regexp constant #rx"(.*)\\1"; the \ in the regular expression must be escaped to include it in a string or regexp constant. [Source: Regexp Syntax]

In many languages like Perl and Ruby regexes are supported syntactically /\([a-z]+\)/, in others there are optional raw strings, like in Python r"\([a-z]+\)". It seems that Racket doesn't support raw strings, where you don't need to escape backslashes, natively. Is there any method to implement them, a third-party library, a proposal, whatever?

See also:


Solution

  • As Chris mentioned, a custom reader can do this.

    An example of a reader that Racket already supplies, that you could use, is at-exp:

    #lang at-exp racket
    
    @~a{C:\Windows\win.ini}
    ;; "C:\\Windows\\win.ini"
    
    @~a{This is a string
        with newlines.}
    ;; "This is a\nstring with newlines."
    

    I like to use ~a with this because it converts anything to a string, and it's only two characters to type.

    However for your regexp example, you can't use ~a or #rx. Instead you should use regexp:

    @regexp{(.*)\1}
    ;; #rx"(.*)\\1"
    

    In all of these examples, @function{string} is read as (function "string") -- basically. There are some nuances you can read about in the documentation for at-exp and Scribble.