Regex literal expressions


#1

I’d like to request a regex literal feature.

Example:
val pattern2:Regex = /test-\d/i;

The main reason I feel it’s important is that without it, escaping gets unreadable quickly.


#2

Are you aware of raw strings?

val r = Regex("""\d+""")

I never liked special strings for regex like in Perl.


#3

Ah, thanks. For some reason I was thinking raw strings still used \ as an escape character. So the only thing you’d need to escape is the $. I think that’ll be fine.
I liked regex literals in AS3, I miss it :).


#4

Note that escaping the $ in raw strings is done using

"""${'$'}"""

#5

Compared to /\d+/ (in, e.g., Ruby) this is incredibly clunky.


#6

$ usually last character in regexp and it does not need escaping in that position.


#7

I’ve never liked / to delimit regex because regex often contain the similar \. But it is true Regex("""\d+""") is clunky compard to /\d+/. In my opinion """ looks not so good for short strings. Java’s new raw strings would look better here: Regex(`\d+`). Or all in one code block for better comparision:

Regex("""\d+""")
/\d+/
Regex(`\d+`)