Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for getting specific data

Tags:

java

regex

xml

I have a file that can be read as a text box, I would like to get only the data available after

start="n= and end="n=

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 1.0//EN" "SMIL10.dtd">
 <head>
 </head>
     <body>
            <audio start="n=10.815s" end="n=19.914s"/>
 </body>
</xml>

I tried doing the following :

   String startTime = readString.replaceAll(".*start=\"n=|\\s.*", "").trim();
   String endTime = readString.replaceAll(".*end=\"n=|\\s.*", "").trim();
   Log.e("Start Time is :" , startTime);
   Log.e("endTime Time is :" , endTime);

Its working fine, with just getting the start time and end time but it also shows the <?xml tag.

How do I fix this?

like image 548
Adarsh H S Avatar asked Mar 28 '26 05:03

Adarsh H S


1 Answers

I would rather use an XML parser to read this. Regexps aren't suited to parsing XML/HTML etc. You'll find numerous references in SO relating to this.

For Java, DOM and SAX are possibilities, but JDOM might make an easier starting point.

like image 73
Brian Agnew Avatar answered Mar 29 '26 18:03

Brian Agnew