0

If I have a string like "(TIde Pods) OR (Pods) AND (myPods)" than how its split as TIde Pods,Pods,myPods?

scala>val s = "Tide POds OR Pods AND (abc ggk)"
s: String = Tide POds OR Pods AND (abc ggk)

scala>s.split("[( ) OR AND]")
res14: Array[String] = Array(Tide, P, ds, "", "", "", Pods, "", "", "", "", "", abc, ggk)

I want Tide Array(POds,Pods,abc ggk).

1
  • 2
    Can you include more examples for input and desired output? Commented Feb 19, 2014 at 8:29

1 Answer 1

10

Since .split accepts regular expression's patterns you can use the following:

scala> val str = "(TIde Pods) OR (Pods) AND (myPods)"
str: String = (TIde Pods) OR (Pods) AND (myPods)


scala> str.split("[()\\s]|OR|AND").filter(_.nonEmpty)
res2: Array[String] = Array(TIde, Pods, Pods, myPods)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.