Find the last match with Java regex matcher

后端 未结 11 2278
情书的邮戳
情书的邮戳 2020-12-01 14:27

I\'m trying to get the last result of a match without having to cycle through .find()

Here\'s my code:

String in = \"num 123 num 1 num 698 num 19238          


        
11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 14:58

    To get the last match even this works and not sure why this was not mentioned earlier:

    String in = "num 123 num 1 num 698 num 19238 num 2134";
    Pattern p = Pattern.compile("num '([0-9]+) ");
    Matcher m = p.matcher(in);
    if (m.find()) {
      in= m.group(m.groupCount());
    }
    

提交回复
热议问题