Kotlin Regex named groups support

柔情痞子 提交于 2020-01-22 17:50:27

问题


Does Kotlin have support for named regex groups?

Named regex group looks like this: (?<name>...)


回答1:


According to this discussion,

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

Kotlin 1.1 EAP is already available to try.


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

You'll have to use kotlin-stdlib-jre8.




回答2:


As of Kotlin 1.0 the Regex class doesn't provide a way to access matched named groups in MatchGroupCollection because the Standard Library can only employ regex api available in JDK6, that doesn't have support for named groups either.

If you target JDK8 you can use java.util.regex.Pattern and java.util.regex.Matcher classes. The latter provides group method to get the result of named-capturing group match.



来源:https://stackoverflow.com/questions/37088679/kotlin-regex-named-groups-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!