所以我有一个包含一堆天气数据的大文件。我必须将大文件中的每一行分配到其相应的状态文件中。因此,总共将有 50 个具有自己数据的新状态文件。
这个大文件包含大约 100 万行这样的记录:
COOP:166657,'NEW IBERIA AIRPORT ACADIANA REGIONAL LA US',200001,177,553
虽然电台的名称可能会有所不同并且有不同的字数。
这是我使用的正则表达式:
Pattern p = Pattern.compile(".* ([A-Z][A-Z]) US.*");
Matcher m = p.matcher(line);
当我运行我的程序时,仍然存在无法找到模式的行实例。
这是我的程序:
package climate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This program will read in a large file containing many stations and states,
* and output in order the stations to their corresponding state file.
*
* Note: This take a long time depending on processor. It also appends data to
* the files so you must remove all the state files in the current directory
* before running for accuracy.
*
* @author Marcus
*
*/
public class ClimateCleanStates {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out
.println("Note: This program can take a long time depending on processor.");
System.out
.println("It is also not necessary to run as state files are in this directory.");
System.out
.println("But if you would like to see how it works, you may continue.");
System.out.println("Please remove state files before running.");
System.out.println("\nIs the States directory empty?");
String answer = in.nextLine();
if (answer.equals("N")) {
System.exit(0);
in.close();
}
System.out.println("Would you like to run the program?");
String answer2 = in.nextLine();
if (answer2.equals("N")) {
System.exit(0);
in.close();
}
String[] statesSpaced = new String[51];
File statefile, dir, infile;
// Create files for each states
dir = new File("States");
dir.mkdir();
infile = new File("climatedata.csv");
FileReader fr = new FileReader(infile);
BufferedReader br = new BufferedReader(fr);
String line;
System.out.println();
// Read in climatedata.csv
// Probably need to implement ClimateRecord class
final long start = System.currentTimeMillis();
while ((line = br.readLine()) != null) {
// Remove instances of -9999
if (!line.contains("-9999")) {
Pattern p = Pattern.compile("^.* ([A-Z][A-Z]) US.*$");
Matcher m = p.matcher(line);
String stateFileName = null;
if(m.find()){
//System.out.println(m.group(1));
stateFileName = m.group(1);
} else {
System.out.println("Could not find abbreviation");
}
/*
stateFileName = "States/" + stateFileName + ".csv";
statefile = new File(stateFileName);
FileWriter stateWriter = new FileWriter(statefile, true);
stateWriter.write(line + "\n");
// Progress reporting
System.out.printf("Writing [%s] to file [%s]\n", line,
statefile);
stateWriter.flush();
stateWriter.close();
*/
}
}
System.out.println("Elapsed " + (System.currentTimeMillis() - start) + " ms");
br.close();
fr.close();
in.close();
}
}
最佳答案
我认为您需要查看函数,它们断言某些内容应该位于您正在匹配的表达式之前或之后,但不包含在结果中。
(?<= )[A-Z][A-Z](?= US)
(?<= )
前面必须有一个空格
[A-Z][A-Z]
恰好两个大写字母
(?= US)
必须是空格和后面的字母 US
环顾四周可能会更稳健:例如,(?= US) 可能是 (?= US',)。
关于java - 在这些行中查找特定模式的正确正则表达式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907791/