Extract all to list function? VB.NET

Discussion in 'Code Snippets and Tutorials' started by Skyle, Apr 20, 2008.

  1. Skyle

    Skyle Level II

    Joined:
    Aug 13, 2007
    Messages:
    212
    Likes Received:
    7
    Has anybody got a function to extract all from a string to a listbox?

    Cheers.
     
  2. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Not that it would be too hard to loop over an array of Strings (obtained using the MatchAll function) or even modify the MatchAll function itself... However, here it is:
    Code (vb.net):
    1. <div class="vbnet" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> &nbsp; &nbsp;<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> ExtractAllToList<span style="color: #000000;">(</span><span style="color: #FF8000;">ByVal</span> strSource <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> strStart <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> strEnd <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByRef</span> lstList <span style="color: #FF8000;">as</span> ListBox<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> strSearch <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> ctlRegEx <span style="color: #FF8000;">As</span> MatchCollection</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; strSearch = Regex.<span style="color: #0000FF;">Escape</span><span style="color: #000000;">(</span>strStart<span style="color: #000000;">)</span> & <span style="color: #808080;">"(?<text>(\r\n)?.*?(\r\n)?)"</span> & Regex.<span style="color: #0000FF;">Escape</span><span style="color: #000000;">(</span>strEnd<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> Regex.<span style="color: #0000FF;">IsMatch</span><span style="color: #000000;">(</span>strSource, strSearch, RegexOptions.<span style="color: #0000FF;">IgnoreCase</span><span style="color: #000000;">)</span> <span style="color: #FF8000;">Then</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctlRegEx = Regex.<span style="color: #0000FF;">Matches</span><span style="color: #000000;">(</span>strSource, strSearch, RegexOptions.<span style="color: #0000FF;">IgnoreCase</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Else</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return <span style="color: #FF8000;">Nothing</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> strResult<span style="color: #000000;">(</span>ctlRegEx.<span style="color: #0000FF;">Count</span> - <span style="color: #FF0000;">1</span><span style="color: #000000;">)</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Long</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">For</span> i = <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> ctlRegEx.<span style="color: #0000FF;">Count</span> - <span style="color: #FF0000;">1</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lstList.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span>ctlRegEx.<span style="color: #0000FF;">Item</span><span style="color: #000000;">(</span>i<span style="color: #000000;">)</span>.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">(</span><span style="color: #808080;">"text"</span><span style="color: #000000;">)</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Next</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></li></ol></div>