Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala: split string by commnas, ignoring commas between quotes [duplicate]

Possible Duplicate:
Java: splitting a comma-separated string but ignoring commas in quotes

It's easier to show some code

I have the following:

scala> val a = """op1,"op2.1,op2.2",,op4""".split(",")
a: Array[java.lang.String] = Array(op1, "op2.1, op2.2", "", op4)

scala> a.foreach( println )
op1
"op2.1
op2.2"

op4

I'd like to get

scala> val a = """op1,"op2.1,op2.2",,op4""".split(",")
a: Array[java.lang.String] = Array(op1, "op2.1, op2.2", "", op4)

scala> a.foreach( println )
op1
op2.1, op2.2

op4

But I can't figure out what regular expression to use to split the string

-- edit --

I found the answer in this question: Java: splitting a comma-separated string but ignoring commas in quotes

like image 473
opensas Avatar asked Nov 11 '12 21:11

opensas


1 Answers

Split with this regexp, it should work: ,(?=([^\"]*\"[^\"]*\")*[^\"]*$)

like image 72
Zaq Avatar answered Oct 22 '22 04:10

Zaq