number out of string in java

后端 未结 6 1468
谎友^
谎友^ 2020-12-19 06:49

I have something like \"ali123hgj\". i want to have 123 in integer. how can i make it in java?

6条回答
  •  没有蜡笔的小新
    2020-12-19 07:39

    You could probably do it along these lines:

    Pattern pattern = Pattern.compile("[^0-9]*([0-9]*)[^0-9]*");
    Matcher matcher = pattern.matcher("ali123hgj");
    boolean matchFound = matcher.find();
    if (matchFound) {
        System.out.println(Integer.parseInt(matcher.group(0)));
    }
    

    It's easily adaptable to multiple number group as well. The code is just for orientation: it hasn't been tested.

提交回复
热议问题