8
入力->入力!RC +計算!R [1] C [1]

出力では、これらを操作したい:

RCおよびR[1]C [1]

私の試み:

private static void findMatch(String Formula){
         マッチャーm=Pattern.compile( "\\ W(R(\\ [(。+?)\\])?C(\\ [(。+?)\\]))")
         .matcher(式);
        //マッチャーm=Pattern.compile(
        // "\\ W(R(\\ [(。+?)\\])?C)| \\ W(R(\\ [(。+?)\\])?C(\\ [( 。+?)\\])) ")
        // .matcher(formula);
        for(; m.find(); m.reset(formula)){
            System.out.println(m.group(3));
        }

    }

2番目のパターンも検索せず、無限ループに入ります。

これはどうしたの?

4

2 に答える 2

7

Try the following:

String formula = "Input!RC + Calc!R[1]C[1]";
Matcher m = Pattern.compile("\\W(R(\\[(.+?)\\])?C(\\[(.+?)\\])?)").matcher(formula);
while (m.find()) {
    System.out.println(m.group(1));
}

Output:

RC
R[1]C[1]

The main change here is how the loop works, what I have above is the typical way for iterating over matches of a pattern. I am also printing m.group(1) instead of m.group(3) because it is the first group that will contain the entire match except for the !, which I think is what you want.

The only change to your regex here was adding the ? after the second (\\[(.+?)\\]) group to make it optional.

于 2013-02-14T00:39:58.870 に答える
3

If you look at this piece of code:

for (; m.find(); m.reset(formula)) {
    System.out.println(m.group(3));
}

For every loop, it calls successively m.reset(formula), resetting the matcher so that it starts at 0, then m.find(), which look for the next match in the string. Since you called reset() before, the next match is the first match. Therefore you have an infinite loop.

If you want to find all matches in the string, simply write:

while(m.find()) {
    System.out.println(m.group(1));
}
于 2013-02-14T00:40:04.843 に答える