William Reed
07/02/2021, 1:29 PM"<cursor>"
) and it escapes properly?Roukanken
07/02/2021, 1:33 PM"""<cursor>"""
- less escaping needed that way, so it will be more readableWilliam Reed
07/02/2021, 1:34 PMWilliam Reed
07/02/2021, 1:35 PM$
to ${'$'}
using the triple quoteWilliam Reed
07/02/2021, 1:35 PMephemient
07/02/2021, 1:35 PM$
, but luckily in a regex that rarely comes up - it only makes sense are the end of a pattern, where it can't be confused for interpolationRob Elliot
07/02/2021, 1:36 PM$
starts an interpolation in a triple quotes string, and there’s no way to escape it, so instead you have to interpolate the character.William Reed
07/02/2021, 1:36 PMephemient
07/02/2021, 1:37 PM${'$'}
:)William Reed
07/02/2021, 1:39 PMRegex("""foo$""")
show an error in intellij?ephemient
07/02/2021, 1:40 PM$
is legal even if intellij would escape it during pasting:
"""foo.*bar$""" == "foo.*bar\$"
it can't possibly be an interpolation, so it remains a $
William Reed
07/02/2021, 1:41 PMRoukanken
07/02/2021, 1:42 PMephemient
07/02/2021, 1:42 PM$
appears in the middle of a raw string, and if you add identifier chars to the string you'll have to change itVampire
07/02/2021, 1:42 PMit only makes sense are the end of a pattern, where it can't be confused for interpolationNot really. You can use it to match a literal dollar, or in some look-ahead really meaning the end of the string, or in a alteration like
($|\n)
and similar things..ephemient
07/02/2021, 1:43 PM$
can't be confused for interpolation eitherephemient
07/02/2021, 1:45 PM"...".toRegex()
to be nicer to read than Regex("...")
William Reed
07/02/2021, 1:48 PMephemient
07/02/2021, 1:48 PM"""
...
""".toRegex(RegexOption.COMMENTS)
which will ignore spaces in the pattern (use [ ]
or similar to encode space literals), if something is very complex and needs internal indentation or commenting to be readableWilliam Reed
07/02/2021, 1:49 PMephemient
07/02/2021, 1:52 PMVampire
07/02/2021, 1:56 PMuseThat will not work though, that just will give you pattern syntax exception for the unclosed character class. 🙂or similar to encode space literals[ ]
Vampire
07/02/2021, 1:57 PM(?-x: )
should workephemient
07/02/2021, 1:57 PMVampire
07/02/2021, 1:58 PMVampire
07/02/2021, 1:58 PMfun main() {
println("""(?x)[ ]""".toRegex().matches(" "))
}
=>
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 6
(?x)[ ]
^
at java.util.regex.Pattern.error (Pattern.java:1957)
at java.util.regex.Pattern.clazz (Pattern.java:2550)
at java.util.regex.Pattern.sequence (Pattern.java:2065)
Vampire
07/02/2021, 2:00 PM\
or disable comment mode with (?-x: )
Vampire
07/02/2021, 2:00 PMephemient
07/02/2021, 2:02 PMVampire
07/02/2021, 2:03 PMVampire
07/02/2021, 2:03 PMVampire
07/02/2021, 2:11 PM(?xx)
to have the same as in Java where spaces in character classes are ignored too