I wouldn’t have expected that ```"<test>.*&l...
# announcements
s
I wouldn’t have expected that
Copy code
"<test>.*</test>".toRegex()
    .find("aaaa<test>5</test>aaaa<test>4</test>aaaa")
    ?.let { println("match: ${it.value}") }
doesn’t return the minimal first match <test>5</test>, but the longer one <test>5</test>aaaa<test>4</test> given that the documentation says: “Returns the first match of a regular expression” here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/find.html And unfortunately findAll equally just returns a single match instead of two. Is there a way for me to fix this? Playground link: https://pl.kotl.in/HA-C02_Gy
I’m aware that I can build the findAll-functionality by using
indexOf
and
substring
but using regexes would be nicer (I don’t want to reinvent the wheel)
a
that’s more of a question about regexes than Kotlin I think, you want to use
.*?
3
m
yeah regex is greedy by default (it wants to match longest possible sequence first).
.*?
should work
s
yes!!!
.*?
does work 😃👍
a
“greedy” vs “lazy” quantifiers is the keyword
👍 2
m
Stephan you may want to read about that here: https://www.regular-expressions.info/repeat.html, the paragraph “Watch Out for The Greediness!” tells exactly your story.
*
and
?
are greedy quantifiers.