Regex literal expressions

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.

4 Likes

Are you aware of raw strings?

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

I never liked special strings for regex like in Perl.

3 Likes

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

Note that escaping the $ in raw strings is done using

"""${'$'}"""
2 Likes

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

1 Like

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

1 Like

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+`)

Wait… Why are templates valid in raw strings? Aren’t they supposed to me… Y’know, raw? As in WYSIWYG?

About the $, you only need to escape it if followed by a letter:

fun main() {
    //sampleStart
    val escapedLetter = "${'$'}hello"
    val notescapedBracket = "$(hello)"
    val notescapedColon = "$:hello"
    val notescapedComma = "$,hello"
    val notescapedDash = "$-hello"
    val notescapedDot = "$.hello"
    val notescapedQuestionMark = "$?hello"
    val notescapedQuotes = """$"hello"""
    //sampleEnd
    println(escapedLetter)
    println(notescapedBracket)
    println(notescapedColon)
    println(notescapedComma)
    println(notescapedDash)
    println(notescapedDot)
    println(notescapedQuestionMark)
    println(notescapedQuotes)
}

I believe that a literal syntax would make regular expressions much easier to read. Compare:

Constructor syntax Extension syntax Literal syntax
Regex("""\w+=\d+""") """\w+=\d+""".toRegex() /\w+=\d+/
Regex("""version=\d+""", IGNORE_CASE) """\w+=\d+""".toRegex(IGNORE_CASE) /\w+=\d+/i
Regex("""test$""", setOf(IGNORE_CASE, MULTILINE)) """test$""".toRegex(setOf(IGNORE_CASE, MULTILINE)) /test$/im
3 Likes

I’m so in line with what @medium said above.

It would be such a nice feature to be able to write regex literals using ` (aka backtick/grave/backquote) character. Then it would be:

  • Single quote for characters
  • Double quotes for strings
  • Triple double quotes for multiline strings
  • Back quote for regular expressions
val char = 'a'
val str = "bunny"
val reg = `\d+`

Related Kotlin YouTrack issue: https://youtrack.jetbrains.com/issue/KT-1340/literal-regex-support

You can’t use backticks for regex literals because backticks are already in use for naming variables and functions when the names wouldn’t otherwise be valid identifiers:

val `^x(ab){2}` = "This is a valid variable name!"
1 Like