Provide access to named groups of regex match result on JDK8.
#KT-12753 Fixed
This commit is contained in:
@@ -1,5 +1,19 @@
|
||||
package kotlin.internal
|
||||
|
||||
import java.util.regex.MatchResult
|
||||
import java.util.regex.Matcher
|
||||
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "CANNOT_OVERRIDE_INVISIBLE_MEMBER")
|
||||
internal open class JRE8PlatformImplementations : JRE7PlatformImplementations() {
|
||||
|
||||
override fun getMatchResultNamedGroup(matchResult: MatchResult, name: String): MatchGroup? {
|
||||
val matcher = matchResult as? Matcher ?: throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.")
|
||||
|
||||
val range = matcher.start(name)..matcher.end(name)-1
|
||||
return if (range.start >= 0)
|
||||
MatchGroup(matcher.group(name), range)
|
||||
else
|
||||
null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:JvmName("RegexExtensionsJRE8Kt")
|
||||
package kotlin.text
|
||||
|
||||
/**
|
||||
* Returns a named group with the specified [name].
|
||||
*
|
||||
* @return An instance of [MatchGroup] if the group with the specified [name] was matched or `null` otherwise.
|
||||
* @throws [UnsupportedOperationException] if getting named groups isn't supported on the current platform.
|
||||
*/
|
||||
public operator fun MatchGroupCollection.get(name: String): MatchGroup? {
|
||||
val namedGroups = this as? MatchNamedGroupCollection ?:
|
||||
throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.")
|
||||
|
||||
return namedGroups[name]
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package kotlin.text.test
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.*
|
||||
|
||||
|
||||
class RegexTest {
|
||||
@Test fun namedGroups() {
|
||||
val input = "1a 2b 3c"
|
||||
val regex = "(?<num>\\d)(?<liter>\\w)".toRegex()
|
||||
|
||||
val matches = regex.findAll(input).toList()
|
||||
assertTrue(matches.all { it.groups.size == 3 })
|
||||
val m1 = matches[0]
|
||||
|
||||
assertEquals("1", m1.groups["num"]?.value)
|
||||
assertEquals(0..0, m1.groups["num"]?.range)
|
||||
assertEquals("a", m1.groups["liter"]?.value)
|
||||
assertEquals(1..1, m1.groups["liter"]?.range)
|
||||
|
||||
val m2 = matches[1]
|
||||
assertEquals("2", m2.groups["num"]?.value)
|
||||
assertEquals(3..3, m2.groups["num"]?.range)
|
||||
assertEquals("b", m2.groups["liter"]?.value)
|
||||
assertEquals(4..4, m2.groups["liter"]?.range)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user