regex getting the first instance of a number from a string

前端 未结 3 1485
悲哀的现实
悲哀的现实 2021-01-15 03:42

I return the following string from a webpage

Order Number: 1509596 Customer Number: 8

but it coul

3条回答
  •  孤街浪徒
    2021-01-15 03:57

    I can't think of a single line that would accomplish this, but the following should work:

    String orderString = "Order Number: 1509596 Customer Number: 8";
    Pattern p = Pattern.compile("[0-9]+");
    Matcher m = p.matcher(orderString);
    if (m.find())
    {
      String orderNr = m.group();
      System.out.println(orderNr);
    }
    

提交回复
热议问题