fork download
  1. object Main extends App {
  2. val stringWithoutIndex = "object[i].base.base_x[i]"
  3. val indexValues = List(0,1)
  4. val strRegex = """(?<=\[)i(?=])"""
  5. val res = indexValues.foldLeft(stringWithoutIndex) { (s, row) =>
  6. s.replaceFirst(strRegex, row.toString)
  7. }
  8. println(res)
  9. }
Success #stdin #stdout 0.4s 2181632KB
stdin
Standard input is empty
stdout
object[0].base.base_x[1]