简体   繁体   中英

extracting long/float numbers from a string in java

I have input like this ==>
2 book at 12.99
4 potato chips at 3.99

I want to extract the numeric values from each line and store them in variables for example in the line.. 2 book at 12.99 i want to extract Qauntity =2 and Price =12.99 from the given string

You can use:

Pattern p = Pattern.compile("(\\d+)\\D+(\\d+(?:.\\d+)?)");
Matcher mr = p.matcher("4 potato chips at 3.99");
if (mr.find()) {
    System.out.println( mr.group(1) + " :: " + mr.group(2) );
}

OUTPUT:

4 :: 3.99

Regex

(\d+)[^\d]+([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)

正则表达式可视化

Debuggex Demo


Description (Example)

/^(\d+)[^\d]+([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$/gm
^ Start of line
1st Capturing group (\d+)
    \d 1 to infinite times [greedy] Digit [0-9]
Negated char class [^\d] 1 to infinite times [greedy] matches any character except:
    \d Digit [0-9]
2nd Capturing group ([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)
    Char class [+-] 0 to 1 times [greedy] matches:
        +- One of the following characters +-
    Char class [0-9] 1 to 3 times [greedy] matches:
        0-9 A character range between Literal 0 and Literal 9
    (?:,?[0-9]{3}) Non-capturing Group 0 to infinite times [greedy]
        , 0 to 1 times [greedy] Literal ,
    Char class [0-9] 3 times [greedy] matches:
        0-9 A character range between Literal 0 and Literal 9
    (?:\.[0-9]{2}) Non-capturing Group 0 to 1 times [greedy]
        \. Literal .
    Char class [0-9] 2 times [greedy] matches:
        0-9 A character range between Literal 0 and Literal 9
$ End of line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Capture Group 1: Contains the Quantity

Capture Group 2: Contains the Amount


Java

try {
    Pattern regex = Pattern.compile("(\\d+)[^\\d]+([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\\.[0-9]{2})?)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        for (int i = 1; i <= regexMatcher.groupCount(); i++) {
            // matched text: regexMatcher.group(i)
            // match start: regexMatcher.start(i)
            // match end: regexMatcher.end(i)
        }
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

Note: This java is just an example, I don't code in Java

You can use MessageFormat class. Below is the working example:

MessageFormat f = new MessageFormat("{0,number,#.##} {2} at {1,number,#.##}");
try {
    Object[] result = f.parse("4 potato chips at 3.99");
    System.out.print(result[0]+ ":" + (result[1]));
} catch (ParseException ex) {
    // handle parse error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM