Structured Processing via String Split

Question

I'm new to Java and to regex in particular I have a CSV file that looks something like:

col1,col2,clo3,col4

word1,date1,date2,port1,port2,....some amount of port

word2,date3,date4,

....

What I would like is to iterate over each line (I suppose I’ll do it with simple for loop) and get all ports back. I guess what I need is the fetch everything after the two dates and look for,(\d+),? and the group that comes back

My question(s) is:

1)Can it be done with one expression? (meaning: without storing the result in a string and then apply another regex)

2) Can I maybe incorporate the iteration over the lines into the regex?

 

Answer

It’s really a hassle to handle this in Java and regular expression. Assign the first 3 data items to the first 3 columns respectively, and the rest to the 4th column. A two-line SPL is sufficient to realize the algorithm:

 

A

1

=file("file.txt").import@st()

2

=A1.(#1.split@c()).new(~(1):col1,~(2):col2,~(3):col3,~.to(4,).concat@c():col4)

A1: Import the text file file.txt as a one-field table sequence without splitting each field value;

A2: Split each row of A1 into 4 parts, create a table sequence made up of col1, col2, col3 and col4, and assign the 4 parts to them:

undefined