gpt4 book ai didi

java - 为什么我需要调用 find() 两次?

转载 作者:行者123 更新时间:2023-11-30 04:27:01 25 4
gpt4 key购买 nike

我正在编写一个程序来解析一堆数据(您可以在此处获取数据集本身的示例:https://explore.data.gov/Geography-and-Environment/Worldwide-M1-Earthquakes-Past-7-Days/7tag-iwnu)。

下面的类工作得很好,但是我不确定为什么我需要在 parseEarthquake()< 中的每个项目之间额外调用 matcher.find() 时间 方法。这是为什么?这是我必须处理的正常怪癖,还是我错误地设置了我的模式/匹配器?

该方法采用包含其中一行数据的字符串(例如,nc,71958020,1,"Thursday, March 21, 2013 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();

}
}

最佳答案

([^,]*) 更改为 ([^,]+),因为前者将始终匹配 - 即使它只匹配任何内容。

关于java - 为什么我需要调用 find() 两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com