私は正規表現が初めてで、正規表現の量指定子セクションを通過しています。分数について質問*
です。*
数量詞の定義は次のとおりです。
X*
- 文字 X がまったく見つからないか、複数見つかる.*
- 任意の文字列
上記の定義に基づいて、小さなプログラムを作成しました。
public static void testQuantifier() {
String testStr = "axbx";
System.out.println(testStr.replaceAll("x*", "M"));
//my expected output is MMMM but actual output is MaMMbMM
/*
Logic behind my expected output is:
1. it encounters a which means 0 x is found. It should replace a with M.
2. it encounters x which means 1 x is found. It should replace x with M.
3. it encounters b which means 0 x is found. It should replace b with M.
4. it encounters x which means 1 x is found. It should replace x with M.
so output should be MMMM but why it is MaMMbMM?
*/
System.out.println(testStr.replaceAll(".*", "M"));
//my expected output is M but actual output is MM
/*
Logic behind my expected output is:
It encounters axbx, which is any character sequence, it should
replace complete sequence with M.
So output should be M but why it is MM?
*/
}
アップデート:-
改訂された理解によると、出力は として期待されますが、 でMaMMbM
はありませんMaMMbMM
。では、最後に余分な M を取得する理由がわかりませんか?
最初の正規表現に対する私の改訂された理解は次のとおりです。
1. it encounters a which means 0 x is found. It should replace a with Ma.
2. it encounters x which means 1 x is found. It should replace x with M.
3. it encounters b which means 0 x is found. It should replace b with Mb.
4. it encounters x which means 1 x is found. It should replace x with M.
5. Lastly it encounters end of string at index 4. So it replaces 0x at end of String with M.
(文字列の終わりのインデックスも考慮するのは奇妙だと思いますが)
これで最初の部分は明らかです。
また、誰かが 2 番目の正規表現を明確にすることができれば、それは役に立ちます。