Java 在这些行中查找特定模式的正确正则表达式是什么?
所以我有一个大文件,其中包含了大量的天气数据。我必须将大文件中的每一行分配到相应的状态文件中。因此,总共将有50个新的状态文件,其中包含它们自己的数据 大文件包含约100万行记录,如下所示:Java 在这些行中查找特定模式的正确正则表达式是什么?,java,regex,string,matcher,Java,Regex,String,Matcher,所以我有一个大文件,其中包含了大量的天气数据。我必须将大文件中的每一行分配到相应的状态文件中。因此,总共将有50个新的状态文件,其中包含它们自己的数据 大文件包含约100万行记录,如下所示: COOP:166657,'NEW IBERIA AIRPORT ACADIANA REGIONAL LA US',200001,177,553 虽然电台的名称可能不同,字数也可能不同 这是我正在使用的正则表达式: Pattern p = Pattern.compile(".* ([A-Z][A-Z]) U
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();
}
}
取决于要提取的内容,但如果使用类型的模式
Pattern.compile("(.*):(.*),'(.*)',(.*),(.*),(.*)");
Matcher m = p.matcher(line);
if(m.find()) {
// here you can use with i from 1 to 6
m.group(i);
//and access the 6 tokens:
//COOP
//166657
//NEW IBERIA AIRPORT ACADIANA REGIONAL LA US
//200001
//177
//553
}
而不是
“*([A-Z][A-Z])美国。*”
如果某些州没有缩写,请尝试:
“([a-z][a-z])+US”
请特别注意^
行的开头、非贪婪组(.*)
、行的结尾$
、点球和多行
Pattern regex = Pattern.compile("^(.*?):(.*?),'(.*?)',(.*?),(.*?),(.*?)$", Pattern.DOTALL | Pattern.MULTILINE);
正则表达式演示:
Live JAVA示例:
正则表达式解释:
^(.*?):(.*?),'(.*?)',(.*?),(.*?),(.*?)$
Options: Case sensitive; Exact spacing; Dot matches line breaks; ^$ match at line breaks; Default line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) (carriage return and line feed, next line, line separator, paragraph separator) «^»
Match the regex below and capture its match into backreference number 1 «(.*?)»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “:” literally «:»
Match the regex below and capture its match into backreference number 2 «(.*?)»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “,'” literally «,'»
Match the regex below and capture its match into backreference number 3 «(.*?)»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “',” literally «',»
Match the regex below and capture its match into backreference number 4 «(.*?)»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “,” literally «,»
Match the regex below and capture its match into backreference number 5 «(.*?)»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “,” literally «,»
Match the regex below and capture its match into backreference number 6 «(.*?)»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) (carriage return and line feed, next line, line separator, paragraph separator) «$»
我认为您需要环顾函数,它们断言某些内容应该在匹配的表达式之前或之后,但不包括在结果中
(?<= )[A-Z][A-Z](?= US)
(?您可以通过以下方式验证它是否是美国各州的缩写:
\s(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])(?:\sUS'|'))
正如我所见,站点名称被单引号包围,为什么不搜索成对的站点名称?事实证明,有些行不包含状态缩写。我会根据站点名称输出文件,但必须按状态分组。您知道如果字符串为是两个字母而不是“我们”,还是只做正则表达式?或者有更快的方法来实现这一点?拆分可能会更快,但正则表达式引擎也非常快:为了区分两者的不同,请编写两个定时测试。请注意,您可以从命令行使用awk和sed等工具组合轻松筛选文件。