Finding all of the matching substrings, not only the “most extended” one

后端 未结 5 1863
甜味超标
甜味超标 2020-12-06 06:02

The code

String s = \"y z a a a b c c z\";
Pattern p = Pattern.compile(\"(a )+(b )+(c *)c\");
Matcher m = p.matcher(s);
while (m.find()) {
    System.out.pri         


        
5条回答
  •  有刺的猬
    2020-12-06 06:08

    You will need a lazy quantifier.

    Please try the following:

    Pattern p = Pattern.compile("(a )+(b )+((c )*?)c");
    

    Please also notice, that I grouped "c" once again, since I think that's what you want. Otherwise you would find arbitrarily many spaces, but not "c".

提交回复
热议问题