1

I'm trying to split a string every time there is a comma in it.

    String myString = "\"Shire, Middle Earth\",Hobbits, J.R.R Tolkien";

My string, when printed out is: "Shire, Middle Earth",Hobbits, J.R.R. Tolkien

Notice that there is a space between Shire and Middle Earth

When I do the following...

    String[] myString = line.split(",");

It counts the comma between the Shire and Middle Earth as a comma to split the data a (as it should). How can I get it to "ignore" that comma?

JavaJew22
  • 83
  • 1
  • 5
  • 1
    Simply put, never use a delimiter that might occur within your data. If there is a chance that a comma might appear within a token, then don't use a comma to separate tokens. – Aurand Apr 25 '13 at 05:02
  • Unfortunately, my string is coming from a file that uses commas to separate the data :'( I would definitely use a different delimeter if it were possible! – JavaJew22 Apr 25 '13 at 05:04

1 Answers1

3

try this:

line.split(",(?!\\s)")

This is lookahead in regex. You can see this link here

Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97