4

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
  • Try this way: `val regex = """(\d+) (\S+)""".r("Id", "Name")` – GoodDok Aug 30 '19 at 11:23
  • 1
    @Wiktor Stribiżew I don't find this as duplicate, this question seems to be about named capture groups not working at all when calling API methods in specific order, but the linked 7y question is about a bug when using named capture groups with look behinds – tlegutko Aug 30 '19 at 12:22
  • The code [does not work](https://rextester.com/GQUB75750)., it is a dupe of https://stackoverflow.com/questions/3029657/scala-regex-named-capturing-groups – Wiktor Stribiżew Aug 30 '19 at 13:07
  • @WiktorStribiżew It works here https://scastie.scala-lang.org/vC7Y8NCmQaSZdHgQl0n2JA with scala 2.12.8. Your example uses scala 2.11.7, so it seems that it's inconsistent between versions. – Krzysztof Piekutowski Aug 30 '19 at 13:30
  • But it does not work with 2.13.0, so perhaps, there was just some sort of a bug in 2.12.8. – Wiktor Stribiżew Aug 30 '19 at 13:33
  • 1
    So you are saying that it's a bug that named groups work in 2.12.8 and it's a feature that they no longer work in 2.13? I would say it's the other way around, it's regression, wouldn't you agree? – Krzysztof Piekutowski Aug 30 '19 at 13:43
  • Hi, I'm facing the same issue. Did you get a solution? @KrzysztofPiekutowski – Sathyamoorthy R May 03 '21 at 06:26
  • 1
    @SathyamoorthyR scala regex is a buggy wrapper over java regex. I ignored the scala wrapper and used the original java API. – Krzysztof Piekutowski May 06 '21 at 11:07

0 Answers0