Greedy mode is also called maximum matching. X?, /tr>abb", maybe the result you are expecting is to match "
Greedy mode (Greedy):
The quantity indicator defaults to greedy mode, unless otherwise indicated. The expression in greedy mode will continue to match until it cannot be matched. If you find that the expression matching results are not as expected, it is most likely because - you thought the expression would only match the first few characters, but in fact it is a greedy pattern, so it will continue to match.
Greedy and non-greedy, plus? is non-greedy:
var s = '1023000'.match(/(\d+)(0*)/); s ["1023000", "1023000", ""] var s = '1023000'.match(/^(\d+)(0*)$/); s ["1023000", "1023000", ""] var s = '1023000'.match(/^(\d+?)(0*)$/); s ["1023000", "1023", "000"] var s = '1023000'.match(/(\d+?)(0*)/); s ["10", "1", "0"]
public void test51(){ String str = "aaa\"bbb\"ccc\"ddd\"eee"; System.out.println(str); str = str.replaceAll("\"(.*)\"", "@"); System.out.println(str); }
Output:
aaa"bbb"ccc"ddd"eee aaa@eee
Example 2:
@Test public void test52(){ String str = "aaa\"bbb\"ccc\"ddd\"eee"; System.out.println(str); str = str.replaceAll("\"(.*?)\"", "@"); System.out.println(str); }
Output:
aaa"bbb"ccc"ddd"eee aaa@ccc@eee
The above is an example Analyze the content of greedy pattern matching of regular expressions in Java programs. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!