如果我有"(TIde Pods) OR (Pods) AND (myPods)"
之类的字符串而不是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)
我想要Tide Array(POds,Pods,abc ggk)
。
答案 0 :(得分:10)
由于.split接受regular expression's模式,您可以使用以下内容:
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)