Kotlin Regex Group Support

Does Kotlin support the support of named regex groups?

A named group of regular expressions looks like this: (?<name>...)

+8
regex regex-group kotlin
source share
2 answers

As in Kotlin 1.0, the Regex class does not provide a way to access mapped named groups in MatchGroupCollection , because the standard library can only use the api regular expression available in JDK6, which does not have support for the named groups either.

If you are targeting JDK8, you can use the java.util.regex.Pattern and java.util.regex.Matcher classes. The latter provides a group method to get the result of a named-capture group match.

+7
source share

According to this discussion ,

This will be supported in Kotlin 1.1. https://youtrack.jetbrains.com/issue/KT-12753

Kotlin 1.1 EAP can already be tried.


 """(\w+?)(?<num>\d+)""".toRegex().matchEntire("area51")!!.groups["num"]!!.value 

You need to use kotlin-stdlib-jre8 .

+10
source share

All Articles