What is the equivalent of Regex-replace-with-function-evaluation in Java 7?

后端 未结 4 1976
终归单人心
终归单人心 2020-12-01 12:07

I\'m looking for a very simple way of getting the equivalent of something like the following JavaScript code. That is, for each match I would like to call a certain

4条回答
  •  無奈伤痛
    2020-12-01 12:42

    Since Java 9 Matcher.replaceAll:

    Pattern.compile("\\S+").matcher("Hello World!")
            .replaceAll(mr -> "" + mr.group().length());
    

    The parameter freely named mr is a MatchResult, with access methods like mr.group(1) or mr.end() - mr.start().

提交回复
热议问题