12

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

Community
  • 1
  • 1
opensas
  • 52,870
  • 69
  • 227
  • 340

1 Answers1

16

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

Zaq
  • 1,198
  • 1
  • 8
  • 18