2014-02-19 39 views
0

如果我有像"(TIde Pods) OR (Pods) AND (myPods)"這樣的字符串比如何將其拆分爲TIde Pods,Pods,myPods我如何在scala中以多個分隔符分割字符串?

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)

+2

你能包括輸入和期望的輸出更多的例子? – 2014-02-19 08:29:48

回答

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) 
相關問題