Java - explain this regular expression (",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1)

spaga

I am separating a string "foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy"" by commas but want to keep the commas in the quotes. This question was answered in this Java: splitting a comma-separated string but ignoring commas in quotes question but it fails to fully explain how the poster created this piece of code which is:

line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);

OK so I do understand some of what is going on but there is a bit that is confusing me. I know the first comma is for matching.

Then

        (?= 

is a forward search.

Then the first part is grouped

  ([^\"]*\"[^\"]*\"). 

This where I get confused. So the first part

  [^\"]* 

means that beginning of any line with quotes separate tokens zero or more times.

Then comes \". Now is this like opening a quote in string or is it saying match this quote?

Then it repeats the exact same line of code, why?

      ([^\"]*\"[^\"]*\")

In the second part adds the same code again to explain that it must finish with quotes.

Can someone explain the part i am not getting?

M. Shaw

[^\"] is any string without ". \" matches ". So basically ([^\"]*\"[^\"]*\") matches a string that contains 2 " and the last character is ".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Related Related

HotTag

Archive