用于提取由分号分隔的元素的定界符
delimiter to extract elements separated by semicolon
我的原始数据格式示例:
196 242X 3
186 B302 3
22 BO377 1
为了提取数据,我沿分隔符 (\t
) 拆分:
EventDAO dao = new SimpleFileRatingDAO(inputFile, "\t");
但是,我的数据格式发生了变化,变成了这样:
"196";"242X";"3"
"186";"B302";"3"
"22";"BO377";"1"
是否有我可以用来提取数据的正则表达式,与我以前的方法类似?
您的新 reg Exp 应该与两个分隔符匹配(如果它们没有出现在数据中)
"[\t|;]"
SimpleFileRatingDAO 似乎允许正则表达式:
它使用调用 String.split(str) :
的对象 class
enter code hereEventDAO dao = new SimpleFileRatingDAO(inputFile, "[\t|;]");
导致:
myLine.split("[\t|;]")
如果您的数据可能包含这些分隔符,但仅在引号之间:只有当逗号前面有零或偶数个引号时,您才需要在逗号上拆分:
"[\t|;](?=([^\"]*\"[^\"]*\")*[^\"]*$)"
Thanks to Bart Kiers who inspire me.
尝试使用简单的字符串操作,如拆分和替换:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Example {
class DataBean{
private String info1;
private String info2;
private String info3;
public DataBean(String info1, String info2, String info3){
this.info1 = info1;
this.info2 = info2;
this.info3 = info3;
}
public String getInfo1() {return info1;}
public void setInfo1(String info1) {this.info1 = info1;}
public String getInfo2() {return info2;}
public void setInfo2(String info2) {this.info2 = info2;}
public String getInfo3() {return info3; }
public void setInfo3(String info3) {this.info3 = info3;}
}
/**
* @param args
* @throws IOException
*
* TODO: do better exc-handling
*/
public static void main(String[] args) throws IOException {
/*
* data.txt:
*
* "196";"242X";"3"
* "186";"B302";"3"
* "22";"BO377";"1"
*/
File dataFile = new File("data.txt");
FileInputStream fis = new FileInputStream(dataFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String strLine;
List<DataBean> data = new ArrayList<DataBean>();
while ((strLine = br.readLine()) != null) {
String[] rawData = strLine.replace("\"", "").split(";");
DataBean dataBean = new Example().new DataBean(rawData[0], rawData[1], rawData[2]);
data.add(dataBean);
}
for (DataBean dataBean : data) {
System.out.println(dataBean.getInfo1() + "-" + dataBean.getInfo2() + "-" + dataBean.getInfo3());
}
br.close();
}
我的原始数据格式示例:
196 242X 3
186 B302 3
22 BO377 1
为了提取数据,我沿分隔符 (\t
) 拆分:
EventDAO dao = new SimpleFileRatingDAO(inputFile, "\t");
但是,我的数据格式发生了变化,变成了这样:
"196";"242X";"3"
"186";"B302";"3"
"22";"BO377";"1"
是否有我可以用来提取数据的正则表达式,与我以前的方法类似?
您的新 reg Exp 应该与两个分隔符匹配(如果它们没有出现在数据中)
"[\t|;]"
SimpleFileRatingDAO 似乎允许正则表达式: 它使用调用 String.split(str) :
的对象 classenter code hereEventDAO dao = new SimpleFileRatingDAO(inputFile, "[\t|;]");
导致:
myLine.split("[\t|;]")
如果您的数据可能包含这些分隔符,但仅在引号之间:只有当逗号前面有零或偶数个引号时,您才需要在逗号上拆分:
"[\t|;](?=([^\"]*\"[^\"]*\")*[^\"]*$)"
Thanks to Bart Kiers who inspire me.
尝试使用简单的字符串操作,如拆分和替换:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Example {
class DataBean{
private String info1;
private String info2;
private String info3;
public DataBean(String info1, String info2, String info3){
this.info1 = info1;
this.info2 = info2;
this.info3 = info3;
}
public String getInfo1() {return info1;}
public void setInfo1(String info1) {this.info1 = info1;}
public String getInfo2() {return info2;}
public void setInfo2(String info2) {this.info2 = info2;}
public String getInfo3() {return info3; }
public void setInfo3(String info3) {this.info3 = info3;}
}
/**
* @param args
* @throws IOException
*
* TODO: do better exc-handling
*/
public static void main(String[] args) throws IOException {
/*
* data.txt:
*
* "196";"242X";"3"
* "186";"B302";"3"
* "22";"BO377";"1"
*/
File dataFile = new File("data.txt");
FileInputStream fis = new FileInputStream(dataFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String strLine;
List<DataBean> data = new ArrayList<DataBean>();
while ((strLine = br.readLine()) != null) {
String[] rawData = strLine.replace("\"", "").split(";");
DataBean dataBean = new Example().new DataBean(rawData[0], rawData[1], rawData[2]);
data.add(dataBean);
}
for (DataBean dataBean : data) {
System.out.println(dataBean.getInfo1() + "-" + dataBean.getInfo2() + "-" + dataBean.getInfo3());
}
br.close();
}