java regex quantifiers

后端 未结 6 2133
春和景丽
春和景丽 2021-01-12 08:00

I have a string like

String string = \"number0 foobar number1 foofoo number2 bar bar bar bar number3 foobar\";

I need a regex to give me th

6条回答
  •  礼貌的吻别
    2021-01-12 08:20

    because .* is a greedy pattern. use .*? instead of .*

    Pattern pattern = Pattern.compile("number\\d+(.*?)(number\\d+)");
    Matcher matcher = pattern.matcher(string);
    while(matcher.find();){
        out(matcher.group());
    }
    

提交回复
热议问题