0

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!

epoche
  • 409
  • 1
  • 4
  • 5
  • You need to look at the Javadoc for `java.util.regex.Matcher` and examples of its use – g00se Apr 23 '22 at 14:30
  • Do you need the last matching group of the regex? – Praveen Apr 23 '22 at 14:43
  • gOOse - I've looked at java.util.regex.Matcher for Java SE10. There are methods to get the text that was matched, but I want to know which named capturing group that was matched. In the example above, if the input was 123 I want to know it was group F1. – epoche Apr 24 '22 at 16:38
  • Praveen - The form of the RE is A|B|...|C where each of A, B, C etc is a named group. As soon as a match is found for an alternative it is returned. I.e. if something is found it will be the last match because the RE parser will stop at that point. If you are matching A|B you don't try to match B if you've found A. – epoche Apr 24 '22 at 16:45

0 Answers0