Edit 1: @GoodDok I can't use val regex = """(\d+) (\S+)""".r("Id", "Name")
because regex pattern and matched string are supplied by user. Number of groups and naming is custom every time.
Edit 2: @Wiktor Stribiżew The question is different than Scala regex Named Capturing Groups. Here you can clearly see that the Named Capturing Groups do work in Scala but only in special circumstances. Working example:
val regex = """(?<Id>\d+) (?<Name>\S+)""".r
val sequence = regex.findAllMatchIn("10 admin\n11 guest").toSeq
// `sequence.length` breaks the underlying Match object
//println(sequence.length)
val mached = sequence.head
println(mached.group(2)) // works fine
println(mached.group("Name")) // also works when`sequence.length` wasn't executed
What doesn't work:
This code used to find multiple matches in a string, it worked for over a year, but recently when accessing named capturing groups it started throwing java.lang.IllegalStateException: No match found
.
val regex = """(?<Id>\d+) (?<Name>\S+)""".r
val sequence = regex.findAllMatchIn("10 admin\n11 guest").toSeq
// `sequence.length` breaks the underlying Match object
println(sequence.length)
val mached = sequence.head
println(mached.group(2)) // works fine
println(mached.group("Name")) // throws `java.lang.IllegalStateException: No match found` if `sequence.length` was executed
When sequence.length
is removed it works fine so it seems that getting number of matches somehow breaks the Match objects.
The same happens when I try to use list instead of sequence.
val matchList = sequence.toList
println(matchList.head.group("Id")) // throws `java.lang.IllegalStateException: No match found`
Is it illegal or undefined to check the number of matches and then use them? What could be the cause of the exception and the change in behavior?
I don't recall updating java recently, my environment is:
~ java -version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing
~ uname -r
4.15.0-58-generic