1

Java では、Pattern と Matcher を使用して、一連の文字列内の ".A (数値)" のすべてのインスタンスを検索し、数値を取得しています。

ファイル内の単語の 1 つが "PAMX" で、数字が 0 を返すため、問題が発生します。ファイルの残りの部分は処理されません。さまざまな正規表現を使用してみましたが、「PAMX」の出現を超えて次の「.A (数値)」に進むことができません。

for (int i = 0; i < input.size(); i++) {

Pattern pattern = Pattern.compile("\\.A\\s\\d+");
Matcher matcher = pattern.matcher(input.get(i));

while (matcherDocId.find())
    {   
            String matchFound = matcher.group().toString();
            int numMatch = 0;
            String[] tokens = matchFound.split(" ");
            numMatch = Integer.parseInt(tokens[1]); 
            System.out.println("The number is: " + numMatch);
    }
}
4

1 に答える 1

1

あなたのための短いサンプル:

Pattern pattern = Pattern.compile("\\.A\\s(\\d+)"); // grouping number
Matcher matcher = pattern.matcher(".A 1 .A 2 .A 3 .A 4 *text* .A5"); // full input string
while (matcher.find()) {
    int n = Integer.valueOf(matcher.group(1)); // getting captured number - group #1
    System.out.println(n);
}
于 2012-10-07T05:30:28.880 に答える