[英]How do you create an array to store regex string matches in java?
我正在嘗試獲取一個存儲這種形式的數據的文件:
Name=”Biscuit”
LatinName=”Retrieverus Aurum”
ImageFilename=”Biscuit.png”
DNA=”ITAYATYITITIAAYI”
並使用正則表達式進行閱讀以找到有用的信息; 即字段及其內容。
我已經創建了正則表達式,但是在任何給定時間我似乎只能得到一個匹配項,而是想將文件中每一行的每個匹配項放在它們自己的字符串索引中。
這是我到目前為止的內容:
Scanner scanFile = new Scanner(file);
while (scanFile.hasNextLine()){
System.out.println(scanFile.findInLine(".*"));
scanFile.nextLine();
}
MatchResult result = null;
scanFile.findInLine(Constants.ANIMAL_INFO_REGEX);
result = scanFile.match();
for (int i=1; i<=result.groupCount(); i++){
System.out.println(result.group(i));
System.out.println(result.groupCount());
}
scanFile.close();
MySpecies species = new MySpecies(null, null, null, null);
return species;
非常感謝你的幫助!
希望我能正確理解您的問題...這是一個來自Oracle網站的示例:
/*
* This code writes "One dog, two dogs in the yard."
* to the standard-output stream:
*/
import java.util.regex.*;
public class Replacement {
public static void main(String[] args)
throws Exception {
// Create a pattern to match cat
Pattern p = Pattern.compile("cat");
// Create a matcher with an input string
Matcher m = p.matcher("one cat," +
" two cats in the yard");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
// Loop through and create a new String
// with the replacements
while(result) {
m.appendReplacement(sb, "dog");
result = m.find();
}
// Add the last segment of input to
// the new String
m.appendTail(sb);
System.out.println(sb.toString());
}
}
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.