Getting a double out of a string

前端 未结 8 1380
温柔的废话
温柔的废话 2020-12-10 15:15

i have a string containing the following: \"Did It Your Way, 11.95 The History of Scotland, 14.50, Learn Calculus in One Day, 29.95\" is there any way to get the doubles fro

8条回答
  •  醉酒成梦
    2020-12-10 15:33

    String text = "Did It Your Way, 11.95 The History of Scotland, 14.50, Learn Calculus in One Day, 29.95";
    
    List foundDoubles = Lists.newLinkedList();
    
    String regularExpressionForDouble = "((\\d)+(\\.(\\d)+)?)";
    Matcher matcher = Pattern.compile(regularExpressionForDouble).matcher(text);
    while (matcher.find()) {
        String doubleAsString = matcher.group();
        Double foundDouble = Double.valueOf(doubleAsString);
        foundDoubles.add(foundDouble);
    }
    
    System.out.println(foundDoubles);
    

提交回复
热议问题