如何在java中创建一个数组来存储正则表达式字符串匹配?

如何在java中创建一个数组来存储正则表达式字符串匹配?,java,regex,arrays,java.util.scanner,Java,Regex,Arrays,Java.util.scanner,我正在尝试获取存储此表单数据的文件: Name=”Biscuit” LatinName=”Retrieverus Aurum” ImageFilename=”Biscuit.png” DNA=”ITAYATYITITIAAYI” 并用正则表达式读取它以查找有用的信息;即字段及其内容 我已经创建了正则表达式,但是在任何给定的时间,我似乎只能获得一个匹配项,而是希望将文件中每一行中的每个匹配项放在它们自己的字符串索引中 以下是我目前掌握的情况: Scanner scanFile = new

我正在尝试获取存储此表单数据的文件:

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;
Scanner scanFile=新扫描仪(文件);
while(scanFile.hasNextLine()){
System.out.println(scanFile.findInLine(“.”);
scanFile.nextLine();
}
MatchResult结果=null;
scanFile.findInLine(Constants.ANIMAL_INFO_REGEX);
结果=scanFile.match();

对于(int i=1;i我希望我正确理解您的问题…以下是来自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());
    }
}

希望这能有所帮助……

如果问题出现,也可以使用您的正则表达式。可能重复“否”,这一点根本不适用。