'Retrieve name from a matching named capturing group in Java/Kotlin regex
I have a Python application that tokenises a line using an RE for each token format. Say the formats were [0-9]+ or [A-Z][A-Z0-9]* or + they might get bundled into: (?P<F1>[0-9]+)|(?P<F2>[A-Z][A-Z0-9]*)|(?P<F3>\+)
Assuming only one group matches a given input, its name can be obtained in Python by requesting lastgroup but I can't see how to do the same in Java or Kotlin. The only (unsatisfactory) solution I've found is loop through all the names and test which has captured something. Is there a better way?
My apologies if this is a trivial question. I retired about 15 years ago my coding isn't up to much these days!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
