3

違いは何ですか:

[xyz]

[x|y|z]

もしあれば?[] と | の両方 代替案を指定します。

次のコードは、まったく同じ結果を出力します。

String string = "the x and the y and the z and the nothing";
evaluatePattern(Pattern.compile("\\w*[xyz]\\w*"), string);
evaluatePattern(Pattern.compile("\\w*[x|y|z]\\w*"), string);
4

6 に答える 6

0

[xyz]は実質的に と同等ですが(?:x|y|z)、内部表現が異なる場合があります。2番目はおそらく遅いです。キャプチャグループを削除するように変更しました。

于 2013-06-24T20:24:14.300 に答える