rp3105 rp3105 - 18 days ago 6
Scala Question

Removing values after particular character from rdd in scala

I have following input:

(A,123#3A,B,C,D,134#wer,E,242#wer)


Is there a way to to get following output using filter/replace/trim or any other function.

(A,123,B,C,D,134,E,242)

Answer

Your question is not completely clear.

If you mean that your input is a list of strings then you can do:

val input = Seq("A","123#3A","B","C","D","134#wer","E","242#wer")

input.map(_.split("#").head)

but if you mean that your input it one string then you can do:

val input2 = "(A,123#3A,B,C,D,134#wer,E,242#wer)"

val Pattern = "\\(([a-zA-Z\\d,#]*)\\)".r
input2 match {
  case Pattern(str) => "(" + str.split(",").map(_.split("#").head).mkString(",") + ")"
}
Comments