结构化解析,拆分字符串,该串用正则表达式很麻烦
【问题】
I'm new to Java and to regex in particular I have a CSV file that look 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 every thing 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?
【回答】
使用Java+正则表达式处理这个实在是很麻烦。把数据的第1-3项各分一列,从第4项开始不管有多少项都分为一列,用SPL只要2句就可以实现这个思路了:
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:读取文本文件file.txt的内容,不拆分字段,读成单字段串构成的序表。
A2:将A1每一行拆分成4部分,然后创建一个由col1、col2、col3和col4构成的序表,结果如下图: