jeudi 22 janvier 2015

How to use Java Pattern to get string between two lines

Wanted to get the String between two lines like below.



<td class="name">Creation date:</td>
<td>2014-11-24<span class=comment> (59 day(s) ago)</span></td>


I need to get the "2014-11-24" from above two lines.


Below is the Sample code.



public String find_cdate(String line)
throws FileNotFoundException, IOException
{
try
{
String date = "";

String pattern = "<td class=name>Creation date:</td>.*?<span class=comment>";
Pattern patt = Pattern.compile(pattern, Pattern.DOTALL);


int counter = 0;


Matcher m = patt.matcher(line);
while (m.find())
{
counter++;
int start = m.start(0);
int end = m.end(0);

String s = line.substring(start, end);
String[] rs = s.split("<.*?>");
for (int i = 0; i < rs.length; i++)
{
date = date + rs[i];
date = date.trim();
}
date = date.substring(14).trim();
}
System.out.println("\nCdate:" + date);
return date;
}
catch (Exception e) {}
return "";
}


This is not working as it looks like regex i used is not correct. Can you please help me how i can create a regex to get the data b/w above two lines.


Thanks in advance for the help.


Aucun commentaire:

Enregistrer un commentaire