Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
Split string by repeatable delimiter in Java/Kotlin

I am splitting a string by a repeatable delimiter, and am also intended to keep the delimiters as well.

val str = "xxoooooooxxoxoxooooo"
val reg = Regex("(?<=x+)|(?=x+)")
var list = str.split(reg)
println(list) 

The output is [, x, x, ooooooo, x, x, o, x, o, x, ooooo], though I would like to get

[xx, ooooooo, xx, o, x, o, x, ooooo]

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

val str = "xxoooooooxxoxoxooooo"
val reg =  Regex("o+|x+").findAll(str).map { it.value }.toList()
println(reg)
//[xx, ooooooo, xx, o, x, o, x, ooooo]
over 4 years ago · Santiago Trujillo Report

0

Actually, it is not correct to use + quantifier inside a lookbehind in Java's regex patterns, this is not a documented supported feature. It does not throw exception because internally it is translated into {1,0x7FFFFFFF} and Java's regex supports constrained-width lookbehind patterns. However, this quantifier in both the lookbehind and lookahead makes no difference as these are non-consuming patterns, and the regex engine still checks each position inside a string for a pattern match.

You can use

(?<=x)(?=o)|(?<=o)(?=x)

See a Kotlin demo:

val str = "xxoooooooxxoxoxooooo"
val reg = Regex("(?<=x)(?=o)|(?<=o)(?=x)")
var list = str.split(reg)
println(list) 
// => [xx, ooooooo, xx, o, x, o, x, ooooo]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!