【问题】
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构成的序表,结果如下图:

博主在学习Java和正则表达式时遇到一个问题,他有一个CSV文件,需要提取每行中日期后的所有端口号。他想知道是否能通过一个正则表达式一次性完成,或者是否可以在遍历文件时结合正则来实现。解决方案建议使用编程方式而非单纯正则,例如SPL(一种编程语言)可以轻松实现这一需求。
686

被折叠的 条评论
为什么被折叠?



