我是 regex 的新手,我正在阅读regex quantifier 部分。我有一个关于*
量词的问题。下面是*
量词的定义:
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.
(虽然我觉得考虑字符串结尾的索引很奇怪)
所以第一部分现在很清楚了。
此外,如果有人可以澄清第二个正则表达式,那将很有帮助。