Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Parse HTML block of code

I have the following piece of HTML code which I need to parse to retrieve the player name and the runs he has scored. In this case it's 'Ross Taylor' and 9. What's the best way to do parse this info? Don't want to use an HTML parser. Is REGEX the best way (I know people are dead against this! But I just want these 2 bits of info and hence don't want to use a parser)? I've been racking my brains on how I should figure out where the player name is in the html file and the consequent row which has the runs scored. The HTML comment part below is a hard coded one. I can reach this place. Then retrieve the name between the tags. Is this a good way to do it? Also how do I retrieve the runs part in the immediate next row?


<!-- <a href="javascript:void(0);" onClick="return showHwkTooltip(this, 'lvpyrbat1');" class="livePlayerCurrent">*Luke Woodcock</a>-->

<a href="/icc_cricket_worldcup2011/content/current/player/38920.html" target="_blank" class="livePlayerCurrent" title="view the player profile for Ross Taylor">
*Ross Taylor
</a>    <span style="margin-left:5px;" title="left-hand bat">(lhb)</span >

   </td >
   <td><b>9</b></td>
   <td>9</td>
   <td>1</td>
   <td>0</td>
   <td>100.00</td>
   <td></td>
   <td colspan="3" align="left"><span class="batStyl">striker</style></td>
   <td></td>
   <td colspan="8"></td>
  </tr>

Please let me know if you need more info.

Regards, Sam

like image 533
sammydude Avatar asked May 22 '26 00:05

sammydude


2 Answers

What's the best way to do parse this info?

Use an HTML parser.

Don't want to use an HTML parser.

I disagree.

Is REGEX the best way

No.

like image 191
CommonsWare Avatar answered May 24 '26 12:05

CommonsWare


Please consider using the proper tool for the job, e.g., a html/xml parser not regex.

If you really want to do it using regex you can try the following out:

Extract score

  (?<=\\<b\\>)\\d+(?=\\</b\\>)

Extract player name

  (?<=\\>)[^\\<]+(?=\\</a\\>)

The second regex assumed you sanitized the xml by removing the anchortag between comment tags.

 <!-- ... -->

What it does it extract the value within any anchortag. This is one of the fundamental restrictions when using regex, it isn't context-aware.

like image 28
Johan Sjöberg Avatar answered May 24 '26 14:05

Johan Sjöberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!