开发者

Conversion from json to decimal

I have a JSON string.

{开发者_JS百科"bounds": {"south west":{ "lng":74.1475868, "lat": 31.366689}, "northeast": { "lng":74.85623 ,"lat": 32.5698746}}

I want to get integers with decimal values using regular expression in Java.


JSON is not a regular language, and can therefore not be parsed by plain regular expressions (and parsing it with non-regular extensions of regular expressions is extremely complicated). Instead, use a Java JSON library.


Don't use regex to parse structured documents. There are way better ways to do it. Now in case someone forced you to use regex to do this job by pointing a gun in your head you could use this :

try {
    Pattern regex = Pattern.compile("[-+]?\\b[0-9]*\\.?[0-9]+\\b");
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        // matched text: regexMatcher.group()
        // match start: regexMatcher.start()
        // match end: regexMatcher.end()
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

This will catch all numbers integer or not negative or positive with optional integer part.

Note it will not match numbers with scientific notation!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜