无法使用具有文本逗号csv的值分割字符串

问题描述

我正在使用这个正则表达式,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$),它被here所证明并且被许多人和我自己所尝试。

但是,当我使用它分割.csv文件时,它会在使用它的.split()上不断给出Arrayindexoutofboundsexception。之前的分割没有问题。这是我的代码

package csvsplit;


import java.io.IOException;


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

public class FreqMapper extends Mapper<LongWritable,Text,IntWritable>{
    
    @Override
    public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
        
        if(key.get()==0) {
            return;
        }else {
            
            String line = value.toString();
            Text channel_name = new Text(line.split(",")[3]); //it splits perfectly
            IntWritable likes = new IntWritable(Integer.parseInt(line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")[8])); //arrayindexoutofboundsexception happens here
            context.write(channel_name,likes);
        }
        
        
    }
    
}

错误图片

error

有什么主意如何防止这种情况发生?谢谢您的阅读。

更新:

update

正则表达式有效。我可以遍历split()的结果。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)