# Regex literal expressions

**URL:** <https://discuss.kotlinlang.org/t/regex-literal-expressions/1938>\
**Category:** Language Design\
**Created:** [August 28, 2016, 6:18pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938 "2016-08-28T18:18:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [August 28, 2016, 6:18pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/1 "2016-08-28T18:18:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [August 28, 2016, 7:00pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/2 "2016-08-28T19:00:00Z")

</div>

Are you aware of raw strings?

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

```

I never liked special strings for regex like in Perl.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [August 28, 2016, 8:29pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/3 "2016-08-28T20:29:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![herman](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@herman](https://discuss.kotlinlang.org/u/herman)\
**Post date:** [October 6, 2017, 9:59am UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/4 "2017-10-06T09:59:43Z")

</div>

Note that escaping the $ in raw strings is done using

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

```

---

<div class="post-metadata">

**Author:** ![reitzig](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/reitzig/32/6970_2.png) [@reitzig](https://discuss.kotlinlang.org/u/reitzig)\
**Post date:** [November 23, 2018, 3:02pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/5 "2018-11-23T15:02:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [November 24, 2018, 12:01pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/6 "2018-11-24T12:01:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [November 29, 2018, 1:28pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/7 "2018-11-29T13:28:16Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![BenLeggiero](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/benleggiero/32/2306_2.png) [@BenLeggiero](https://discuss.kotlinlang.org/u/BenLeggiero)\
**Post date:** [May 9, 2019, 1:33pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/8 "2019-05-09T13:33:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [May 9, 2019, 8:22pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/9 "2019-05-09T20:22:20Z")

</div>

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

```run-kotlin
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)
}

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Wolf](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/daniel_wolf/32/6554_2.png) [@Daniel\_Wolf](https://discuss.kotlinlang.org/u/Daniel_Wolf)\
**Post date:** [July 10, 2019, 7:53am UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/10 "2019-07-10T07:53:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahozad](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mahozad/32/10670_2.png) [@mahozad](https://discuss.kotlinlang.org/u/mahozad)\
**Post date:** [December 20, 2023, 4:42pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/11 "2023-12-20T16:42:27Z")

</div>

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

```kotlin
val char = 'a'
val str = "bunny"
val reg = `\d+`

```

---

<div class="post-metadata">

**Author:** ![mahozad](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mahozad/32/10670_2.png) [@mahozad](https://discuss.kotlinlang.org/u/mahozad)\
**Post date:** [December 20, 2023, 5:09pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/12 "2023-12-20T17:09:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![klitos](https://avatars.discourse-cdn.com/v4/letter/k/258eb7/32.png) [@klitos](https://discuss.kotlinlang.org/u/klitos)\
**Post date:** [December 20, 2023, 5:55pm UTC](https://discuss.kotlinlang.org/t/regex-literal-expressions/1938/13 "2023-12-20T17:55:38Z")

</div>

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:

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

```
