0

I am having a string ">= 50.80" I am trying split logical operator and float value using below

val result = ">= 50.80"
val Pattern = "(<[=>]?|==|>=?|\\&\\&|\\|\\|)".r
val Pattern(operator) = result
println(operator)

Error:

Exception in thread "main" scala.MatchError: >= 50.80 (of class java.lang.String).

Mario Galic
  • 45,265
  • 6
  • 51
  • 87
Arvinth
  • 124
  • 3
  • 21
  • 1
    What doesn't work? It splits on `>=`, so it will be splitted into an array like `["", " 50.80"]`, making index `0` the empty string...? – Narigo Jul 14 '20 at 19:57
  • Okie. I made a mistake. I want to split into [">=" , " 50.80"]. I want to support multiple logical operators such `,==,<=,=>`. Sometime space will not present between logical operator and float value – Arvinth Jul 14 '20 at 20:02

1 Answers1

1

According to this answer, negative lookbehind and lookahead can be leveraged to keep delimiters.

val result = ">= 50.80"
val str_split = result.split("(?<=(<[=>]?|==|>=|\\&\\&|\\|\\|))|(?=(<[=>]?|==|>=|\\&\\&|\\|\\|))")

for (v <- str_split) {
  println(v)
}
Narigo
  • 2,719
  • 3
  • 18
  • 29