0

テキストファイルから読み取ろうとしていますが、テキストファイルは次のとおりです。

champ[34] = "ABC"; w1[34] = "Nick"; w2[34] = "SAM"; w3[34] = "Ronald";  
w4[34] = "Richeal"; w5[34] = "George"; w6[34] = "Sarah"; w7[34] = "Karin";  
w8[34] = "Chris"; w9[34] = "Christina";

10個のテキストフィールドがあります。テキストファイルを読みたいのですが、リーダーが (") があるかどうかを確認すると、ABC を最初の textField に配置し始め、2 番目の textField に Nick を配置します...

また、テキストファイルにも1行しかありませんが、テキストファイルに保存するときに「\ n」で設定した3行で表示されます。

とにかくそれを行うことはありますか??

4

1 に答える 1

0

すべてのファイル コンテンツを 1 つの文字列変数に読み取ります。次に、正規表現を使用して、一致するサブ文字列を取得します。パターンは次のようなものである必要があります: get shortest text between =(0 個から多数のスペースを使用できます)" と "(0 個から多数のスペースを使用できます);

詳細については、 http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.htmlを参照してください。

例えば:

 String str = "champ[34] = \"ABC\"; w1[34] = \"Nick\"; w2[34] = \"SAM\"; w3[34] = \"Ronald\"; w4[34] = \"Richeal\"; "
            + "w5[34] = \"George\"; w6[34] = \"Sarah\"; w7[34] = \"Karin\"; w8[34] = \"Chris\"; w9[34] = \"Christina\";";


    Pattern pattern = Pattern.compile("=\\s*\".*?\"\\s*;");
    Matcher matcher = pattern.matcher(str);

    while (matcher.find()) {
        System.out.print("Start index: " + matcher.start());
        System.out.print(" End index: " + matcher.end() + " ");
        System.out.println("group:" + matcher.group());
    }
于 2013-01-29T14:23:08.593 に答える