.net - Pulling out specific text in an html file using vb.net -


i trying 3 values large html file. thought use substring method, informed position of data may change. basically, in following code need pick out "total number of records: 106", "number of records imported:106", , "number of records rejected: 0"

<b>total number of records : </b>106</font><br><font face="arial" size="2"><b>number of records imported : </b>106</font><br><font face="arial" size="2"><b>number of records rejected : </b>0</font> 

i hope clear enough. in advance!

simple string operations indexof() , substring() should plenty job. regular expressions approach that'd take less code (and may allow more flexibility if html tags can vary), mark twain say, didn't have time short solution, wrote long 1 instead.

in general you'll better results around here showing you've @ least made reasonable attempt first , showing got stuck. time...here go. :-)

private shared function getmatchingcount(allinputtext string, textbefore string, textafter string) integer?      'find first occurrence of text before desired number     dim startposition integer = allinputtext.indexof(textbefore)      'if text before not found, return nothing     if startposition < 0 return nothing      'move start position end of text before, rather beginning.     startposition += textbefore.length      'find first occurrence of text after desired number     dim endposition integer = allinputtext.indexof(textafter, startposition)      'if text after not found, return nothing     if endposition < 0 return nothing      'get string found @ start , end positions     dim textfound string = allinputtext.substring(startposition, endposition - startposition)      'try converting string found integer     try         return cint(textfound)     catch ex exception         return nothing     end try end function 

of course, it'll work if text before , after same. if use driver console app (but without shared, since it'd in module then)...

sub main()     dim alltext string = "<b>total number of records : </b>106</font><br><font face=""arial"" size=""2""><b>number of records imported : </b>106</font><br><font face=""arial"" size=""2""><b>number of records rejected : </b>0</font>"""""      dim totalrecords integer? = getmatchingcount(alltext, "<b>total number of records : </b>", "<")     dim recordsimported integer? = getmatchingcount(alltext, "<b>number of records imported : </b>", "<")     dim recordsrejected integer? = getmatchingcount(alltext, "<b>number of records rejected : </b>", "<")      console.writeline("total: {0}", totalrecords)     console.writeline("imported: {0}", recordsimported)     console.writeline("rejected: {0}", recordsrejected)     console.readkey() end sub 

...you'll output so:

total: 106

imported: 106

rejected: 0


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -