What does this split line mean in Scala?

I have a problem with specific Scala code where I found this dividing line. Before I used only dividing lines, for example:

var newLine = line.split(",") 

But what does this split mean?

 var newLine2 = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)") 

The line I need to split is as follows:

 1966, "Green, Green Grass of Home", Tom Jones, 850000 

Thanks in advance!

+6
source share
2 answers

The string inside the split method defines the regular expression . The group (?=([^\"]*\"[^\"]*\")*[^\"]*$) Is a positive statement for the forecast . This means comma-separated, but only if the pattern ([^\"]*\"[^\"]*\")*[^\"]*$ follows the comma.

 ([^\"]* # a series of non double quote characters \" # a double quote [^\"]* # a series of non double quote characters \") # a double quote * # repeat that whole group 0 or more times [^\"]*$ # a series of non double quote characters till the end of the string 

means that it will only be separated by commas if there is an equal number of double quotes after the comma, so in other words, it splits only if the comma is not inside the double quotes . (This will work for as long as there are only a couple of quotes in the line.)

+11
source

This is a regular expression ("RegEx"), see http://en.wikipedia.org/wiki/Regular_expression for an explanation

+2
source

Source: https://habr.com/ru/post/946383/


All Articles