Java 为什么我需要调用find()两次?
我正在编写一个程序来解析一组数据(您可以在这里获得数据集本身的示例:) 下面的类工作得非常好,但是我不确定为什么需要调用Java 为什么我需要调用find()两次?,java,regex,Java,Regex,我正在编写一个程序来解析一组数据(您可以在这里获得数据集本身的示例:) 下面的类工作得非常好,但是我不确定为什么需要调用matcher.find()方法中的每个项之间的额外时间。为什么呢?这是我必须处理的一个正常怪癖,还是我设置的图案/匹配器不正确? 该方法获取包含一行数据的字符串(例如,nc,71958020,1,“星期四,2013年3月21日17:13:34 UTC”,38.8367,-122.8298,1.4,2.60,28,“北加利福尼亚州”),并为数据返回地震对象 import jav
matcher.find()
方法中的每个项之间的额外时间。为什么呢?这是我必须处理的一个正常怪癖,还是我设置的图案/匹配器不正确?
该方法获取包含一行数据的字符串(例如,nc,71958020,1,“星期四,2013年3月21日17:13:34 UTC”,38.8367,-122.8298,1.4,2.60,28,“北加利福尼亚州”
),并为数据返回地震对象
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Earthquake {
String src="xx";
String eqid="00000000";
short version;
long dateTime;
float lat, lon;
float mag, dep;
short nst;
String region="Nowhere";
private Earthquake(){
date.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private static DecimalFormat
coords = new DecimalFormat( "##0.0000" ),
magnitude = new DecimalFormat( "###0.0" ),
depth = new DecimalFormat( "###0.00" );
private static SimpleDateFormat date = new SimpleDateFormat("'\"'EEEE', 'MMMM' 'dd', 'yyyy' 'HH':'mm':'ss' 'zzz'\"'");
// Src, Eqid, Version, Datetime, Lat, Lon, Magnitude, Depth, NST, Region;
public static Earthquake parseEarthquake(String string){
Earthquake result = new Earthquake();
Matcher matcher = Pattern.compile("(\".*?\")|([^,]*)").matcher(string);
try {
matcher.find(); result.src = matcher.group();
matcher.find(); matcher.find(); result.eqid = matcher.group();
matcher.find(); matcher.find(); result.version = Short.parseShort(matcher.group());
matcher.find(); matcher.find(); result.dateTime = date.parse(matcher.group()).getTime();
matcher.find(); matcher.find(); result.lat = coords.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.lon = coords.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.mag = magnitude.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.dep = depth.parse(matcher.group()).floatValue();
matcher.find(); matcher.find(); result.nst = Short.parseShort(matcher.group());
matcher.find(); matcher.find(); result.region = matcher.group();
} catch (ParseException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
}
return result;
}
public String toString(){
StringBuffer buf = new StringBuffer();
buf.append(src);
buf.append(','); buf.append(eqid);
buf.append(','); buf.append(version);
buf.append(','); date.format(dateTime, buf, new FieldPosition(0));
buf.append(','); coords.format(lat, buf, new FieldPosition(0));
buf.append(','); coords.format(lon, buf, new FieldPosition(0));
buf.append(','); magnitude.format(mag, buf, new FieldPosition(0));
buf.append(','); depth.format(dep, buf, new FieldPosition(0));
buf.append(','); buf.append(nst);
buf.append(','); buf.append('"'); buf.append(region); buf.append('"');
return buf.toString();
}
}
将
([^,]*)
更改为([^,]+)
,因为前者总是匹配的-即使它只是不匹配任何东西。顺便说一句,正如您可能已经猜到的,我还有一大堆其他东西要添加到类中。它还没有完全完成。如果要使用库读取CSV文件格式,可以删除大部分代码(和bug)。@IvanNevostruev该库是java标准库的一部分吗?我打算最终将其作为processing.org草图的一部分使用,因此如果它不是标准库,那么这可能会非常困难,因为它不是标准Java的一部分。但与编写自己的库相比,使用经过良好测试的库可以节省更多的时间。@SotiriosDelimanolis因为条目的日期部分有逗号