Using Regular Expressions to Extract a Value in Java

前端 未结 13 1259
失恋的感觉
失恋的感觉 2020-11-22 13:08

I have several strings in the rough form:

[some text] [some number] [some more text]

I want to extract the text in [some number] using the

13条回答
  •  执笔经年
    2020-11-22 13:38

    In Java 1.4 and up:

    String input = "...";
    Matcher matcher = Pattern.compile("[^0-9]+([0-9]+)[^0-9]+").matcher(input);
    if (matcher.find()) {
        String someNumberStr = matcher.group(1);
        // if you need this to be an int:
        int someNumberInt = Integer.parseInt(someNumberStr);
    }
    

提交回复
热议问题