regex getting the first instance of a number from a string

前端 未结 3 1486
悲哀的现实
悲哀的现实 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:50

    if you change orderNumber.replaceAll("[^0-9]", "")

    to

    orderNumber.replaceAll("[^0-9]", ",") hence

    you will get ,1509596,8 as an answer,

    I think this is probably help you to solve ans.

    0 讨论(0)
  • 2021-01-15 03:52
    String firstNumber = str.replaceAll("^\\D*(\\d+).*", "$1");
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题