0
嘗試使用java提取兩個模式(標記)之間的界限,通過將值存儲在變量中我可以做到這一點。如何使用java模式提取模式之間的線?
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RunExampleTest{
private Pattern addressPattern = Pattern.compile(ADDRESS_PATTERN);
private Matcher matcher;
//Alternative
/*private Pattern addressPattern =
Pattern.compile(ADDRESS_PATTERN, Pattern.DOTALL);*/
private static final String ADDRESS_PATTERN = "(?s)Address 1:\\s(.*)Address 2:";
public static void main(String[] args) {
String data = "Testing... \n" +
"Address 1: 88 app 2/8\n" +
"superman taman, puchong\n" +
"36100, Malaysia\n" +
"Address 2: abc" +
"testing end";
RunExampleTest obj = new RunExampleTest();
List<String> list = obj.getAddress(data);
System.out.println("Test Data : ");
System.out.println(data + "\n");
System.out.println("Address Resut : " + list);
}
private List<String> getAddress(String data){
List<String> result = new ArrayList<String>();
matcher = addressPattern.matcher(data);
while (matcher.find()) {
result.add(matcher.group(1));
}
return result;
}
}
但不是從變量讀取我需要從文件的開始和結束模式之間獲取數據。
例如輸入文件:
Address 1: xyz
89/23
Pin -12
Address 2: abc address
Address 1: xyz
89/23
Pin -12
Address 2: abc address
用下面的,
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
matcher = addressPattern.matcher(line);
while(matcher.find())
{
System.out.println(line);
}
}
輸出文件:
xyz
89/23
Pin -12
xyz
89/23
Pin -12
試圖通過線讀線,但沒有幫助。任何人都可以幫助
我正在嘗試讀取文件,即非結構化數據。需要根據起始標記(地址1 :)和結束標記(地址2 :)從多行文件中提取。 –
我已經想通了。我無法弄清楚的是,(和我引用)*「輸入是由一條記錄還是多條記錄組成」*。 –
多條記錄。 –