assylias
assylias

Reputation: 328795

groupValues only returns last occurrence of repeating pattern

println(Regex("(A|B)+").matchEntire("AB")!!.groupValues)

prints [AB, B] but I would have expected the result to be [AB, A, B].

Upvotes: 1

Views: 213

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627334

The output you get is expected. (A|B)+ matches and captures A, places the match value into Group 1 buffer and then captures B (as the + matches one or more times) and places the value into Group 1 re-writing the A inside.

So, the only way to get what you need is capturing both A or B, but it is not that easy.

See

println(Regex("(?:(A)|(B))+").matchEntire("AB")!!.groupValue‌s)

See the Kotlin demo.

It outputs [AB, A, B], as the the whole string matches the regex, the value is added as the first item, the Capture 1 is added as the second item, and Capture 2 is the third item.

But if you have a longer string like 123abc456def and you have (?:(\d+)|(\p{L}+))+, you will lose all the captures but last. So, for 123abc456def input, you will get [123abc456def, 456, def].

However, you should provide more details in case you need to match such strings.

Upvotes: 2

Related Questions