-6

this is the text :

<meta itemprop="duration" content="PT4M32.80000000000001S"> 

i want to extract PT4M32.80000000000001S and convert it into numeric. I had tried Integer.parseInt() but it gives me NumberFormatException. Would you help please.

  • What is the expected output of "turning `PT4M32.80000000000001S` into numeric"? Do you want a function that returns 32.80000000000001? – Martin J.H. Apr 26 '15 at 10:01
  • Is "PT4M32.80000000000001S" supposed to be an [ISO 8601 duration](https://en.wikipedia.org/wiki/ISO_8601#Durations)? – Kenster Apr 26 '15 at 12:42
  • yes it's ISO 8601 duration.then after knowing about ISO 8601 duration.So is there any regex to extract the duration and convert in into numeric? – QTarik Liverpool Apr 26 '15 at 13:51
  • Have a look at http://stackoverflow.com/questions/27851832/how-do-i-parse-an-iso-8601-formatted-duration-using-moment-js – Wiktor Stribiżew Apr 26 '15 at 19:52

1 Answers1

2

It depends on what number you want to extract, and if the context is fixed. Say, you always have PT4M before the double value of 32.80000000000001 that you want to extract.

Here is a sample program:

import java.util.regex.*;
public class HelloWorld{

     public static void main(String []args){
        String str = "PT4M32.80000000000001S";
        Pattern ptrn = Pattern.compile("(?<=PT4M)[\\d.]+");
        Matcher matcher = ptrn.matcher(str);
        if (matcher.find()) {
           System.out.println(matcher.group(0));
           Double fl = Double.parseDouble(matcher.group(0));
           System.out.println(fl);
        }
     }
}

Output:

32.80000000000001                                                                                                                                                   
32.80000000000001 
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476